What is Docker
Docker is a CI/CD tool that utilises containers for code deployment. Docker containers are instantiated from a composition of many docker images. Ranging from a simple OS level image to more advanced automatic build and deployment images. Docker images are generated from simple Dockerfiles which lay out the commands that the container runs either before or after its instantiation. Docker is useful when wanting to run the same piece of code on different machine architectures as it works much like a virtual machine would.
This section describes some of the command line tools that docker offers, as well as some useful tricks.
Generally the first thing that you should do when running docker is to login. This is done with the
command. You will then be prompted for your docker username and password.
Running a container
Once logged in, the next step is to run an image. This is done via the
docker run imagename
command. If the image is not currently downloaded to your computer then it will be downloaded from the docker hub. You can update a previously downloaded image by using the
docker pull imagename
command is often run with several flags. A particularly useful flag is the
flag, which lets you set a name for the container that you can use in other commands later on.
can map an exposed port in the container to a system port, i.e., running several of the same container and mapping different ports to the containers without having to change the code.
lets you run the container as a background process.
Much like in a linux terminal, you can use
to show the currently running containers. The flag
shows all of the containers on the system.
You can remove a container with the
docker rm container
. If the container is currently running, you can either stop it with
or use the
flag to forcefully remove the container. You can remove all of the containers on the machine indescriminately by running the following
docker rm $(docker ps -a -q)
. Alternatively you can use the
flag when running the image so that the container is removed when it exits.
To see a list of the currently downloaded images on the computer run the
command. To remove a specific image you can use the
docker rmi imagename
Arguably the most important part of docker is the ability to create your own images with ease. This is done by using a dockerfile and then calling the
command. The easiest way of doing this is inside the same directory that the dockerfile is in. It is important to give the image a tag so that it can be easily referred to later on.
docker build -t imagename .
Once the image has been built, you can link it to a remote repo in the docker hub by using the
docker tag localname remote/name
. The once a link has been made, push the image to the hub
docker push remote/name
Dockerfiles are what docker uses to build the docker images. Each docker file contains a base docker image defined by the
statement. This image is then built upon by using various other rules. The most common other rules being
will execute a system command for the OS defined by the base image.
will copy files from the build computer to the container.
Below is an example of how to create a golang dockerfile via a ubuntu image.
FROM ubuntu MAINTAINER Mark Hillman # Add the go path to the system ENV GOPATH /go # Copy over the files to the gopath dir ADD . /go/src/github.com/imitablerabbit/go-project # Update and install golang via apt RUN apt-get update && \ apt-get install -y golang && \ go install github.com/imitablerabbit/go-project # Set the entrypoint of the container. ENTRYPOINT /go/bin/go-project # Expose the port EXPOSE 8080
There is a much better way though. You can just use the pre-existing golang images. The following example is based on the Go blog docker examples.
FROM golang:1.6-onbuild MAINTAINER Mark Hillman EXPOSE 80