Docker tutorial: Get started with Docker




docker run -i -t --name apache_web ubuntu /bin/bash

This creates a new container with a unique ID and the name apache_web. It also gives you a root shell because you specified /bin/bash as the command to run. Now, install the Apache web server using apt-get:


apt-get install apache2

Note that you don’t need to use sudo, because you’re running as root inside the container. Note that you do need to run apt-get update, because, again, the package list inside the container is not the same as the one outside of it. (The other instructions inside the container do not require sudo unless explicitly stated.)

The normal apt-get output appears, and the Apache2 package is installed in your new container. Once the install has completed, start Apache, install curl, and test the installation, all from within your container:


service apache2 start
apt-get install curl

curl http://localhost

If you were doing this in a production environment, you’d next configure Apache to your requirements and install an application for it to serve. Docker lets directories outside a container be mapped to paths inside it, so one approach is to store your web app in a directory on the host and make it visible to the container through a mapping.

Create a startup script for a Docker container

Remember that a Docker container runs only as long as its process or processes are active. So if the process you launch when you first run a container moves into the background, like a system daemon, Docker will stop the container. Therefore, you need to run Apache in the foreground when the container launches, so that the container doesn’t exit as soon as it fires up.

Create a script, startapache.sh, in /usr/local/sbin:


apt-get install nano

nano /usr/local/sbin/startapache.sh


(You don’t have to use the nano editor to do this, but it’s convenient.)

The contents of startapache.sh:


#!/bin/bash
. /etc/apache2/envvars
/usr/sbin/apache2 -D FOREGROUND

Save the file and make it executable:


chmod +x /usr/local/sbin/startapache.sh

All this small script does is bring in the appropriate environment variables for Apache and start the Apache process in the foreground.

You’re done modifying the contents of the container, so you can leave the container by typing exit. When you exit the container, it will stop.

Commit the container to create a new Docker image

Now you need to commit the container to save the changes you’ve made:


docker commit apache_web local:apache_web

The commit will save your container as a new image and return a unique ID. The argument local:apache_web will cause the commit to be placed in a local repository named local with a tag of apache_web.

You can see this by running the command docker images:


REPOSITORY   TAG          IMAGE ID       CREATED          SIZE
local        apache_web   540faa63535d   24 seconds ago   233MB
ubuntu       latest       b1e9cef3f297   4 weeks ago      78.1MB

Note that the exact details of your image—the image ID and the size of the container—will be different from my example.

Docker networking basics

Now that you have your image, you can start your container and begin serving pages. Before you do that, let’s discuss how Docker handles networking.

Docker can create various virtual networks used by Docker containers to talk to each other and the outside world:

  • bridge: This is the network that containers connect to by default. The bridge network allows containers to talk to each other directly, but not to the host system.
  • host: This network lets containers be seen by the host directly, as if any apps within them were running as local network services.
  • none: This is essentially a null or loopback network. A container connected to none can’t see anything but itself.

Other network drivers also exist, but these three are most crucial for starting out.

When you want to launch a container and have it communicate with both other containers and the outside world, you need to manually map ports from that container to the host. For the sake of my example, you can do this on the command line when you launch your newly created container:


docker run -d -p 8080:80 --name apache local:apache_web /usr/local/sbin/startapache.sh

The -p switch is used for port mapping. Here, it maps port 8080 on the host to port 80 inside the container.

Once you run this command, you should be able to point a web browser at the IP address of the host and see the default Apache web server page.

You can see the status of the container and the TCP port mappings by using the docker ps command:


CONTAINER ID   IMAGE               COMMAND                  CREATED          STATUS          PORTS             
     NAMES
81d8985d0197   local:apache_web   "/usr/local/sbin/sta…"   13 minutes ago   Up 12 minutes   0.0.0.0:8080->80/tcp   apache

You can also look up the network mappings by using the docker port command, in this case docker port apache


80/tcp -> 0.0.0.0:8080

Note that you could use the -P option on the docker run command to publish all open ports on the container to the host and map a random high port such as 49153 back to port 80 on the container. This can be used in scripting as necessary, but it’s generally a bad idea to do this in production.

At this point, you have a fully functional Docker container running your Apache process. When you stop the container, it will remain in the system and can be restarted at any time via the docker restart command.

Use Dockerfiles to automate Docker image builds

As educational as it is to build Docker containers manually, it is pure tedium to do this repeatedly. To make the build process easy, consistent, and repeatable, Docker provides a form of automation for creating Docker images called Dockerfiles.

Dockerfiles are text files, stored in a repository alongside Docker images. They describe how a specific container is built, letting Docker perform the build process for you automatically. Here is an example Dockerfile for a minimal container, much like the one I built in the first stages of this demo:


FROM ubuntu:latest
RUN apt-get update
RUN apt-get install -y curl
ENTRYPOINT ["/bin/bash"]

If you save this file as dftest in your local directory, you can build an image named ubuntu:testing from dftest with the following command:


docker build -t ubuntu:testing - < dftest

In PowerShell, you’d use this command:


cat .\dftest | docker build -t ubuntu:testing -

Docker will build a new image based on the ubuntu:latest image. Then, inside the container, it will perform an apt-get update and use apt-get to install curl. Finally, it will set the default command to run at container launch as /bin/bash. You could then run:


docker run -i -t ubuntu:testing

Et voilà! You have a root shell on a new container built to those specifications. Note that you can also launch the container with this command:


docker run -i -t dftest

Numerous operators are available to be used in a Dockerfile, such as mapping host directories to containers, setting environment variables, and even setting triggers to be used in future builds. See the Dockerfile reference page for a full list of Dockerfile operators.

Next steps with Docker

There’s much more to Docker than we’ve covered in this guide, but you should have a basic understanding of how Docker operates, a grasp of the key Docker concepts, and enough familiarity to build functional containers. You can find more information on the Docker website including an online tutorial that goes into more granular detail about Docker features.

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img