Docker Command Line Guide
In the last tutorials of the series, we already learned about what Docker is and how we can use Docker images. In this guide, I will create a summary of the most useful commands, allowing you to deploy and customize containers on your servers without any hassle.
Commands for Containers
Docker provides commands that let the user manage any containers that are on the given machine.
|docker ps||Shows all running containers|
|docker ps -a||Shows all containers|
|docker stop <container name>||Stops running container with the given name|
|docker stop $(docker ps -a -q)||Stops all running containers|
|docker rm $(docker ps -a)||Deletes all containers|
Commands for Images
Likewise, we can manage the images on our server using similar commands:
|docker pull <image name>||Pulls a specified Docker image from DockerHub|
|docker run <image name>||Starts a container based on the image that was specified (pulls it automatically if not available locally but on DockerHub)|
|docker run –name <image name>||Starts a container based on the specified image and assigns it the specified name|
|docker run -d <image name>||Starts a container based on the specified image as a background process (it is not logged to current console)|
|docker images||Lists all intermediate images|
|docker images <image name>||Lists all images with the given name|
|docker image prune||Deletes all images that are not being used by a container|
Customizing Docker Containers using the Command Line
Most images from DockerHub have parameters, that can be specified using the command line. These are configured as you start the container using the
docker run command. For the following examples, I will be using the MySQL Docker Image.
Setting Environment Variables
Looking at the MySQL Docker image page on DockerHub, I can see from the environment variable section that I have several options at my disposal, including the mandatory variable MYSQL_ROOT_PASSWORD. Environment variables in Docker are set by passing along every environment variable using the -e parameter.
docker run -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=mydb mysql
The prior command will start a new MySQL container with the specified root password and creates a new database with the specified name.
Exposing and Mapping a Container Port
Containers have ports just like a normal computer and Docker gives us the ability to “map” container ports to those on the host machine. This means that when a user accesses a specified port on our host machine, they are really accessing another port on the given container. It is important to ensure that, when running multiple containers, there is not overlap between the mappings i.e. several containers are mapped to the same port. To map ports from a container, we need to “expose” them first. By default, our MySQL image has port 3306 exposed, which makes sense because this is the port that a fresh installation of MySQL listens on. We can also expose it using the expose parameter in our run command:
docker run -e MYSQL_ROOT_PASSWORD=password --expose 3306 mysql
There is no practical need to replicate this, as our MySQL image has this setting by default. When mapping ports, we need to use the -p parameter. For example: if I wanted to access the database through the port 3307 on my host machine, I would use the following command:
docker run -e MYSQL_ROOT_PASSWORD=password -e --expose 3306 -p 3307:3306 mysql
The port address that is left of the colon is the one for our host machine, the one that is right of the colon is the container port we are trying to access.
Docker provides a feature called volumes, which allows the user to map a directory from the container to the local machine, thus persisting data even after the container is stopped (or even removed entirely). Volumes can be created through the command line:
docker volume create databasedata
Notice how after we run this command, docker creates a new folder named “databasedata” in our current directory. We now want to map that folder (or volume) to the folder of our MySQL container, which stores the actual data (if you didn’t know, MySQL data is stored in the /var/lib/mysql directory on Ubuntu):
docker run -v databasedata:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=password mysql
Just like with port mappings, the directory left of the colon is the path to my volume on the host machine while the directory right of the colon represents the directory containing the data on the container.
Entering a Container
Sometimes, it is necessary to enter the actual container via the command line. This is particularly helpful if you are trying to debug some strange behavior. You can do this by running the command
docker exec -it <container name>/bin/bash. Once inside the container shell, you can interact with it just like you would via ssh. We can exit the container shell by pressing the key combinations ctrl + p and ctrl + q consecutively.
Thanks for reading this post! In the next part of this series, you will learn how to build your own Docker images using Dockerfile.