How to Use Docker Images, Containers, and Dockerfiles in Depth
Traducciones al EspañolEstamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Docker images make it easy to deploy multiple containers without having to maintain the same image across several virtual machines. You can use a Dockerfile to automate the installation and configuration of an image and its dependencies. A Dockerfile is a text file of the commands (which are executed in order) used to automate installation and configuration of a Docker image. This article expands on our guide on How to Use a Dockerfile to Build a Docker Image by covering in-depth utilization of Docker images, containers, and Dockerfiles.
Before You Begin
Familiarize yourself with our Getting Started guide, create and update a Linode, and install Docker. Alternatively, you can quickly deploy an updated, Docker-enabled Linode with the Docker Marketplace App.
Ensure your Linode is secure by following our guide on How to Secure Your Server.
This guide assumes you are comfortable with using the Docker command-line interface (CLI). To learn more about the Docker CLI, check out their documentation.
Review our guide on to become familiar with the basics of Dockerfiles.
Create Your Dockerfile for the Docker Image
Docker requires a working Dockerfile for its builds. Here, we will create a Dockerfile that sets up an Ubuntu image with Apache acting as a web server and using the standard HTTP port 80.
At the command prompt (either via SSH or Lish in the Linode Manager), create and change to a new directory:
mkdir ~/mydockerbuild && cd ~/mydockerbuild
Create an example Dockerfile for your Apache application:
Open the Dockerfile using the text editor of your choice (for this example, we use nano):
Copy the following example into your Dockerfile. This creates a Dockerfile that generates an updated Ubuntu image, sets the maintainer information, installs Apache, opens container port 80, and finally starts an Apache server when run:
- File: apache_dockerfile
1 2 3 4 5 6 7 8 9
FROM ubuntu MAINTAINER John Doe [email protected] ARG DEBIAN_FRONTEND=noninteractive RUN apt-get update RUN apt-get upgrade -y RUN apt-get install apache2 -y RUN apt-get clean EXPOSE 80 CMD ["apache2ctl","-D","FOREGROUND"]
ARG DEBIAN_FRONTEND=noninteractiveinstruction ensures that the subsequent
RUN apt-getcommands execute without requiring additional user input when building images. This instruction could also be written using
ARGto make the environment variable persist in containers that are deployed with the image. Because non-interactivity may not be expected when working within such containers,
ARGis recommended in this case.
Save and close the file.
Build a Docker Image from the Dockerfile
Build an image labelled
apache_imagefrom the Dockerfile using the
docker build ~/mydockerbuild -f apache_dockerfile -t apache_image
Once the build is over and you’re returned to the command prompt, display your system’s available images with the following command:
The output should look like this (the “ubuntu” repository is also available due to the “FROM ubuntu” line in the Dockerfile):
REPOSITORY TAG IMAGE ID CREATED SIZE apache_image latest 7e5c14739da5 7 seconds ago 215MB ubuntu latest 7e0aa2d69a15 6 weeks ago 72.7MB
By default, built images are tagged “latest.” If you want to change the tag, such as to “development”, format the command as follows:
docker build ~/mydockerbuild -f apache_dockerfile -t apache_image:development
Running your Docker Images as Containers
When you execute the
docker run command, you launch a Docker container tied to your terminal session. This is also referred to as running a process in the foreground. When your root process is in the foreground and is tied to a terminal session, your container exits as soon as you close the terminal session. If you want your container to run even after your terminal session is closed, you can run your container in detached mode. This runs your container in the background.
To run the Docker image as a container in detached mode:
Enter the following command to build a container named
apachewith your image, using the
-dargument to ensure your container runs in the background:
docker run --name apache -d apache_image
Once you are back at the command prompt, run the following command to list your active containers and confirm that
apacherunning in the background:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1d5e1da50a86 apache_image "apache2ctl -D FOREG…" 3 minutes ago Up 3 minutes 80/tcp apache
Now you can do your development work with the Apache server and still have access to the command line. However, your container is not publicly accessible as it lacks additional port configurations. In the next section, you will rebuild the container with port configurations that allow you to access the web server. For now, stop the container:
docker stop apache
NoteYou can enter the container ID in place of
apachein the above command.
docker psagain to make sure all your
apachecontainer is no longer running.
Now that the container has been stopped, you can remove it:
docker rm apache
Removing a container in this way deletes all data within the container. If you have made adjustments that you want to carry to a new container, you can instead use
docker committo build a new image that includes your updates:
docker commit apache apache_image_update
Then, you can deploy a new container based on the new
apache_image_updateimage in the next section.
Configure your Docker Container’s Ports
You can use the
run command’s options to configure different aspects of your container. When your container runs on a remote host and serves its application, you can configure its ports to expose the app to users.
For example, you can configure your
apache container to use host port
8080 and container port
80 as with the example command below. Notice the
-d option used in the command to run the container as a detached process.
docker run --name apache -p 8080:80 -d apache_image
The general syntax for this command is the following:
docker run -–name <container name> -p <network port>:<container port> -d <container image label or ID>
Each parameter is described in the following list:
<container name>: Name of the Docker container
<host port>: Host port that is mapped to the container’s open port
<container port>: Port where Docker container listens
<container image name>: Docker image name used for your deployment
Now, navigate to your Linode’s IP address at host port 8080 by navigating to
http://<your Linode's IP address>:8080 in a web browser. You should see the “Apache2 Ubuntu Default Page” served from your Docker container.
CautionWhen deploying containers with port configurations, Docker may also create host firewall rules to allow public access to those containers. This can override or conflict with the host firewall rules you have configured on your Linode.
This guide and How to Use a Dockerfile to Build a Docker Image covered the basics of using Dockerfiles to build images and containers, but they barely scratch the surface of what you can accomplish with Docker. For more information:
visit the official Dockerfile Best Practices documentation for more on Dockerfiles;
and, despite its name, Docker’s Get Started guide is an in-depth tutorial, which leads into even more in-depth guides, such as deploying applications into the cloud and setting up CI/CD (continuous integration and deployment).
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This page was originally published on