don’t dock docker, its great

By | September 3, 2020
Codeception - How to start automatic tests | Droptica Blog

Install docker on your mac

Install docker on your mac using the following instructions. https://docs.docker.com/docker-for-mac/install/

Is it running?

Type in docker version to see if docker is running properly. You should see both the client and a server listed with no errors.

docker version
Client: Docker Engine - Community
 Version:           19.03.12
 API version:       1.40
 Go version:        go1.13.10
 Git commit:        48a66213fe
 Built:             Mon Jun 22 15:41:33 2020
 OS/Arch:           darwin/amd64
 Experimental:      false
Server: Docker Engine - Community
 Engine:
  Version:          19.03.12
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.13.10
  Git commit:       48a66213fe
  Built:            Mon Jun 22 15:49:27 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          v1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

Docker help

If you type in docker --help you will get a list of docker commands and options.

docker --help
Usage:	docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
      --config string      Location of client config files (default "/Users/jbaileyugroup/.docker")
  -c, --context string     Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket(s) to connect to
  -l, --log-level string   Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default "/Users/jbaileyugroup/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/Users/jbaileyugroup/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/Users/jbaileyugroup/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit
Management Commands:
  builder     Manage builds
  config      Manage Docker configs
  container   Manage containers
  context     Manage contexts
  image       Manage images
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes
Commands:
  attach      Attach local standard input, output, and error streams to a running container
  build       Build an image from a Dockerfile
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  diff        Inspect changes to files or directories on a container's filesystem
  events      Get real time events from the server
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  images      List images
  import      Import the contents from a tarball to create a filesystem image
  info        Display system-wide information
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  login       Log in to a Docker registry
  logout      Log out from a Docker registry
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  ps          List containers
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  search      Search the Docker Hub for images
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  version     Show the Docker version information
  wait        Block until one or more containers stop, then print their exit codes
Run 'docker COMMAND --help' for more information on a command

Let create a Nginx server

Start a simple Nginx server by entering docker container run --publish 8080:80 nginx . Press ctrl+c to get back to the terminal.

docker container run --publish 8080:80 nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
bf5952930446: Pull complete 
cb9a6de05e5a: Pull complete 
9513ea0afb93: Pull complete 
b49ea07d2e93: Pull complete 
a5e4a503d449: Pull complete 
Digest: sha256:b0ad43f7ee5edbc0effbc14645ae7055e21bc1973aee5150745632a24a752661
Status: Downloaded newer image for nginx:latest
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up

Go to your browser and enter localhost:8080. Congratulations you have created your first webserver in Docker!

Docker official images

In the previous example we added a Ngnix to our container and ran it. Since this is the first time we used Ngnix you probably noticed it was pulling the image down from the internet to your local computer. Docker can use many different types of images including various languages, databases, or almost anything that can run on a server. You can find a list of the official images here. https://github.com/docker-library/official-images/tree/master/library

You don’t have to start a container to pull down images you want to use. You can pull them yourself using the docker pull [containerName]. Once pulled these images will be used when creating new containers.

What Images do I currently have?

You can get a list of your current images by typing docker images -a. Notice that the Nginx image is 3 weeks old. This means that people at docker had updated the image 3 weeks ago. If you go to https://github.com/docker-library/official-images/tree/master/library you can confirm that the Ngnix image was updated 22 days ago.

docker images -a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              4bb46517cac3        3 weeks ago         133MB

You can delete the image by typing docker rmi [imageName] . This failed for me at first because my container was still running.   

docker rmi nginx      
Error response from daemon: conflict: unable to remove repository reference "nginx" (must force) - container d024b71d525b is using its referenced image 4bb46517cac3

I recommend not removing your images unless you want to reclaim space or want to clean up your Docker environment.

If you want to get the history of the image you can use docker history [imageName]

docker history nginx
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
4bb46517cac3        3 weeks ago         /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon…   0B                  
<missing>           3 weeks ago         /bin/sh -c #(nop)  STOPSIGNAL SIGTERM           0B                  
<missing>           3 weeks ago         /bin/sh -c #(nop)  EXPOSE 80                    0B                  
<missing>           3 weeks ago         /bin/sh -c #(nop)  ENTRYPOINT ["/docker-entr…   0B                  
<missing>           3 weeks ago         /bin/sh -c #(nop) COPY file:0fd5fca330dcd6a7…   1.04kB              
<missing>           3 weeks ago         /bin/sh -c #(nop) COPY file:1d0a4127e78a26c1…   1.96kB              
<missing>           3 weeks ago         /bin/sh -c #(nop) COPY file:e7e183879c35719c…   1.2kB               
<missing>           3 weeks ago         /bin/sh -c set -x     && addgroup --system -…   63.4MB              
<missing>           3 weeks ago         /bin/sh -c #(nop)  ENV PKG_RELEASE=1~buster     0B                  
<missing>           3 weeks ago         /bin/sh -c #(nop)  ENV NJS_VERSION=0.4.3        0B                  
<missing>           3 weeks ago         /bin/sh -c #(nop)  ENV NGINX_VERSION=1.19.2     0B                  
<missing>           5 weeks ago         /bin/sh -c #(nop)  LABEL maintainer=NGINX Do…   0B                  
<missing>           5 weeks ago         /bin/sh -c #(nop)  CMD ["bash"]                 0B                  
<missing>           5 weeks ago         /bin/sh -c #(nop) ADD file:3af3091e7d2bb40bc…   69.2MB  

Some useful docker container commands and parameters

To see a list of all containers running type docker container ls -a

docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
d024b71d525b        nginx               "/docker-entrypoint.…"   14 minutes ago      Exited (0) 13 minutes ago                       gifted_poincare

To stop a container type docker container stop plus the ID or name of the container. You only have to type enough characters of the ID to uniquely identify the container, in my case I typed d02 but d would have worked as well. If you choose to use the name the entire name “gifted_poincare” will have to be entered.

docker container stop d02
d02

Adding the — detach option with run the container and return to the command line.

docker container run --publish 8080:80 --detach nginx
fa5d7012c7ac7a89ea355679007dc267e5b0a055a9cd5fa9eef1dc8aeca8b0a1

When you are detached you cannot see the logs. To see them enter docker container logs [containerName/ID]. Just like the before you only have to type in enough ID information to uniquely identify the container. In this case I just used “f” for the ID.

docker container logs f
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up

Remove containers using the rm command. The container must be stopped first. You can force the container to stop by adding the -f attribute. I believe it is good practice to stop the container first, so you don’t remove a container you want to keep running.

docker container stop f 
f
docker container rm f
f

You can specify a name for your container by adding the --name option as follows.

docker container run --publish 8080:80 --detach --name myfirstserver nginx
c34fc8d388057ff0a15d02d524bc9b50f0e4182aefd985884b295ca6ad5ebd27

You can also get processes info about your container using docker container top [containerName/ID].

docker container top f 
PID                 USER                TIME                COMMAND
3990                root                0:00                nginx: master process nginx -g daemon off;
4044                101                 0:00                nginx: worker process

Details of the container configuration can be listed by typing docker container inspect. This will give you a large JSON string with a bunch of configuration details.

A quick way to get stats and see how your containers are running can be achieved by typing in docker container stats [containerName/ID]. The main metrics shown are CPU and memory usage.

docker container stats
CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
b9c569561b7b        my_db               1.64%               324.4MiB / 1.945GiB   16.29%              936B / 0B           0B / 0B             38
c34fc8d38805        myfirstserver       0.00%               2.055MiB / 1.945GiB   0.10%               2.14kB / 1.27kB     0B / 0B             2

What if you are just playing around and want to just start something quickly and as soon as you stop it the container erases itself. To do this add the –rm option.

docker container run --name tempweb --rm --publish 8084:80 -d nginx
docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
aed468a94e33        nginx               "/docker-entrypoint.…"   51 seconds ago      Up 50 seconds       0.0.0.0:8084->80/tcp   tempweb
docker container stop tempweb
tempweb
docker container ls          
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

MySQL

Next lets create a MySQL server by entering docker container run --publish 3306:3306 --detach --name [yourDBName] -e MYSQL_RANDOM_ROOT_PASSWORD=true mysql

docker container run --publish 3306:3306 --detach --name my_db -e MYSQL_RANDOM_ROOT_PASSWORD=true mysql

To get the password for your MySQL server enter docker container logs [yourDBName] and find the GENERATED ROOT PASSWORD: line.

docker container logs my_db
...
2020-09-05 18:33:48+00:00 [Note] [Entrypoint]: GENERATED ROOT PASSWORD: Eilael2wa7iefai9ukoomaxool2Beiz1
...

Apache server

To start an apache server use “httpd” for the image name not “apache”. I made this mistake a couple of times. :). Plus use port 8081:80 to not conflict with your currently running Ngnix server.

docker container run -d --name my_webserver -p 8081:80 httpd

What containers are running so far

Now that we have several containers running enter docker container ls -a to see them. Pretty cool stuff.

docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
f3620f5fc997        httpd               "httpd-foreground"       12 seconds ago      Up 12 seconds       0.0.0.0:8081->80/tcp                my_webserver
c4b962185d7c        mysql               "docker-entrypoint.s…"   6 minutes ago       Up 6 minutes        0.0.0.0:3306->3306/tcp, 33060/tcp   my_db
c34fc8d38805        nginx               "/docker-entrypoint.…"   2 hours ago         Up 2 hours          0.0.0.0:8080->80/tcp                myfirstserver

Curl

Instead of going to our browser to see our servers running we just type curl to see the html content using curl localhost:8080 and curl localhost:8081.

curl localhost:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
curl localhost:8081
<html><body><h1>It works!</h1></body></html>

Docker documents

Find great documentation for docker here https://docs.docker.com/

Tip of the docker tail

We have just scratched the tip of the docker tail. Have fun. There is so much more to learn. In a future blog we will dive in deeper.