Introduction To Docker And Containers
whatever you are be a good one ~ Abraham Lincoln
About Docker
Docker is an open platform to build, ship and run distributed applications. Docker is not a VM
Useful Terminology
Image
- Your staring point
- what you produce as an output
Container
- A running image
Host
- The machine Docker is installed on
Why Docker?
- Escape dependency hell
- Onboard developers and contributors rapidly
- Implement reliable CI easily
Install Docker
Our first containers
docker run -it ubuntu
The flags -i
is for interactive mode, -t
: is for terminal
Incase the ubuntu
image is unavailable locally, docker will source it from public container registry, by default is dockerhub
sample output:
1
2
3
4
5
6
7
clevinbash@pop-os:~$ docker run -it ubuntu
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
aece8493d397: Pull complete
Digest: sha256:2b7412e6465c3c7fc5bb21d3e6f1917c167358449fecac8176c6e496e5c1f05f
Status: Downloaded newer image for ubuntu:latest
root@872f14919aa2:/#
In the terminal session you can do normal distro based installations i.e apt-get update
and apt-get install <package>
i.e figlet
To exit the container type exit
or ctrl + D
Background containers
docker run -d <imageName>
To check running containers: docker ps
To tail a container logs: docker logs --tail 1 --follow <containerId> or containeName
To stop container docker kill
or docker stop
Restarting and attaching to containers
Docker does not distinguish between background and foreground containers, from its pov all containers are the same.
Attach to a running container: docker attach <containerId>
If you attach to a container running a REPL (Read Eval print loop) hit
Enter
to display the terminal
Understanding docker images
What is an image
An image is a collection of files and some meta data, images are made of layers which can be updated, shared and reused independently. Each layer can add, change and remove files.
building docker images
Automated builds with Dockefile
Dockerfile is a recipe for building a docker image.
- create an empty directory to hold your Dockerfile:
mkdir <myImage>
Inside your directory create the Dockerfile and paste this into the file:
1 2 3
FROM ubuntu RUN apt-get update RUN apt-get install figlet
The keywords i.e
FROM
can be in lowercase, but the standard is to use uppercase, for more information view Dockerfile reference
- To build the image run:
docker build -t <imageName> <context>
1
docker build -t figlet .
The
-t
is a reference to the tag of an image
Advanced Dockerfile
Collapsing layers: it is possible to execte multiple commands in a single step. Alway note that every single docker command will generate a new layer.
1
2
3
RUN apt-get update \
&& apt-get install -y wget \
&& apt-get clean
EXPOSE
instruction tells Docker which ports are to be published in an image, all port are private by default.
1
2
3
EXPOSE 8080
EXPOSE 80 443
EXPOSE 53/tcp 53/udp
COPY
instruction adds files and contents from your host to the image.
1
COPY . /src
You can only reference files and directories inside the build context
ADD
works like copy but with extra features.
- It can get remotefiles
1
ADD http://www.example.com/web.jar /opt/
- ADD will automatically unpack zip files and tar archives
1
ADD ./assets.zip /var/ww/htdocs/assets/
VOLUME
tells Docker that there will be persistent content
1
VOLUME /var/lib/mysql
WORKDIR
sets the working directory for subsequent instructions. It also affects CMD
and ENTRYPOINT
, since it sets the working directory used when starting the container.
1
WORKDIR /src
ENV
specifies environment variables that should be set in any container launched from the image.
1
ENV WEB_PORT 8080
You can also overwride the environmet variables when running docker run
1
docker run -e WEB_PORT=8000 ...
Naming and inspecting containers
Container names must be unique
To inspect an image: docker inspect <imageName>
Example using go template engine:
1
docker inspect --format '{{ .State.Running }}' <ContainerName>
Local development workflow with Docker
📺 Watch Video