Docker Commandline Guide
Tags: docker, beginners, devops, linux
Published: 2019-12-16 07:20:15

<h2>Docker Command Line Guide</h2>
<p>In the last tutorial 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.
<h3>Commands for Containers</h3>
Docker provides commands that let the user manage any containers that are on the given machine.
<table class="table table-striped"><thead><tr><th scope="col" class="col-3">Command</th><th scope="col" class="col-6">Functionality</th></tr></thead><tbody><tr><td>docker ps</td><td>Shows all running containers</td></tr><tr><td>docker ps -a</td><td>Shows all containers</td></tr><tr><td>docker stop &lt;container name&gt;</td><td>Stops running container with the given name</td></tr><tr><td>docker stop $(docker ps -a -q)</td><td>Stops all running containers</td></tr><tr><td>docker rm $(docker ps -a)</td><td>Deletes all containers</td></tr></tbody></table>
<h3>Commands for Images</h3>
Likewise, we can manage the images on our server using similar commands:
<table class="table table-striped"><thead><tr><th scope="col" class="col-3">Command</th><th scope="col" class="col-sm-6">Functionality</th></tr></thead><tbody><tr></tr><tr><td>docker pull &lt;image name&gt;</td><td>Pulls a specified Docker image from DockerHub</td></tr><tr><td>docker run &lt;image name&gt;</td><td>Starts a container based on the image that was specified (pulls it automatically if not available locally but on DockerHub)</td></tr><tr><td>docker run --name &lt;image name&gt;</td><td>Starts a container based on the specified image and assigns it the specified name</td></tr><tr><td>docker run -d &lt;image name&gt;</td><td>Starts a container based on the specified image as a background process (it is not logged to current console)</td></tr><tr><td>docker images</td><td>Lists all intermediate images</td></tr><tr><td>docker images &lt;image name&gt;</td><td>Lists all images with the given name</td></tr><tr><td>docker image prune</td><td>Deletes all images that are not being used by a container</td></tr></tbody></table>
<h3>Customizing Docker Containers using the Command Line</h3>
<p>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 <a href="hub.docker.com/_/mysql">MySQL Docker Image</a>.</p>
<h4>Setting Environment Variables</h4>
<p>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.</p>
<pre><code class="language-bash">docker run -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=mydb mysql</code></pre>
<p>The prior command will start a new MySQL container with the specified root password and creates a new database with the specified name.</p>
<h4>Exposing and Mapping a Container Port</h4>
<p>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:</p>
<pre><code class="language-bash">docker run -e MYSQL_ROOT_PASSWORD=password --expose 3306 mysql</code></pre>
<p>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:</p>
<pre><code class="language-bash">docker run -e MYSQL_ROOT_PASSWORD=password -e --expose 3306 -p 3307:3306 mysql</code></pre>
<p>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.</p>
<h4>Volumes</h4>
<p>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:</p>
<pre><code class="language-bash">docker volume create databasedata</code></pre>
<p>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):</p>
<pre><code class="language-bash">docker run -v databasedata:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=password mysql</code></pre>
<p>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.</p>
<h3>Entering a Container</h3>
<p>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 <code>docker exec -it <container name>/bin/bash</code>. 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.</p>
<p>Thanks for reading this post! In the next part of this series, you will learn how to build your own Docker images using Dockerfile.</p>