[Docker] Getting inside your container
Part 3/5
In this post, we will learn how to get into the containers to do some administrative tasks. By now you would have notice that docker addressing is inspired by computer networking. Remote login is probably one of the holy grail for network engineers; we use them to ssh into routers/servers or basically any devices with remote login enabled for configuration and administration.
Similarly, we can ‘remote login’ to the containers. Docker natively support tty access for us to get into containers; so there’s no need to install ssh within the container to accept remote connection. To do so, we parse the -it option in the cli when using ‘docker container run’.
So here’s a case study. Suppose that we started a nginx web server and named it ‘spiderWEB’. Now, instead of just spinning up the server; we also want to get inside the container so create a new directory in the /home directory called ‘customer_websites’ to store our .php files. The command to achieve this is:
docker container run -p 80:80 --name spiderWEB -it nginx bash
This command start a nginx web server in the interactive mode; and the ‘bash’ allows us to execute our unix command inside the container. Think of the ‘-it’ as the entrance ticket into Disneyland, and bash is the camera you bring in to take pictures. Your cmd should show a prompt indicating that you are already inside the container.
Once you are in, you now have full control on the container and you can do what you would normally do as on a Linux machine. In this example, we will first navigate to the /home directory and create a new directory using the command mkdir then show the newly created directory using ls. To return to docker host, type exit.
cd home
mkdir customer_websites
ls
Here’s a video explaining the solution on this example.
Hooray! Now you have the power to control your containers just like your VMs. Next, we will learn about docker networking which is important if you plan to expose your containers to the public Internet or run them in production.