Docker basics

Share this post!
Docker containers, Dockerfiles and data persistance

This post is a quick how-to guide to solve some fundamental problems I had when using Docker, as you probably will:

  • containers
  • Dockerfiles (with uppercase ‘D’)
  • data persistence
  • clean up unused images and containers
  • quick guide using some templates

I recommend having a look at the Try Docker course on Codeschool for a complete explanation.

Containers

A container is like a VM, but without the operating system. It is a basic piece of Docker and is used to execute software. The following is a list of commands that you can find useful:

  • Start the container based on the image ‘httpd’ version 2.4, and map port 80 on the container to port 25080 on localhost. Now you should be able to access http://localhost:25080/ on your computer.
$ docker container run -p 25080:80 httpd:2.4
  • Get the container’s unique id and a random number (you will get a different name).
$ docker container ls
CONTAINER ID   IMAGE       COMMAND              CREATED          STATUS          PORTS                   NAMES
5963f9920e06   httpd:2.4   "httpd-foreground"   15 minutes ago   Up 15 minutes   0.0.0.0:25080-80/tcp   peaceful_swartz
  • Execute commands on the container (use the container’s name from the previous command).
$ docker container exec peaceful_swartz pwd
/usr/local/apache2
  • Execute a shell
Inside this shell you can execute a command, install packages, etc, as any other shell can do.

$ docker container exec peaceful_swartz /bin/bash

Dockerfiles

A Dockerfile is a text file used by Docker to execute tasks related to a container.

$ vi Dockerfile
FROM httpd:2.4
EXPOSE 80
RUN apt-get update apt-get install -y fortunes
LABEL maintainer=moby-dock@example.com

Now build an image based on this Dockerfile (the last dot on the command indicates that the command will look for the Dockerfile in the same folder as the command is executed):

$ docker image build --tag web-server:1.0 .

Have a look at the images on localhost:

$ docker image ls

Create a container from this image:

$ docker container run -p 80:80 web-server:1.0

Data persistence

Data and files must go to and from the container and localhost.

  • Copy files to a container

 </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>$ docker container cp page.html elegant_noether:/usr/local/apache2/htdocs/</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>

  • Add a line to the Dockerfile to automatically copy files at boot time:
FROM httpd:2.4
EXPOSE 80
RUN apt-get update
install -y fortunes
COPY page.html /usr/local/apache2/htdocs/
LABEL maintainer=moby-dock@example.com

Run the container in the background:

$ docker container run -p 80:80 --detach web-server:1.1

and get the file you just copied:

$ curl localhost:80/page.html
  • Using Volumes

Start a container and share /my-files in the localhost to htdocs on the container:

$ docker run -d -p 80:80 -v /my-files:/usr/local/apache2/htdocs web-server:1.1

We can start a shell and have a look from inside the container:

$ docker container exec -it elegant_noether /bin/bash
$ cd /usr/local/apache2/htdocs

Clean up unused images and containers

Clean up disk space used by exited containers and dangling images:

$ docker rm -v $(docker ps -a -q -f status=exited)
$ docker rmi $(docker images -f dangling=true -q)

Docker and Kali Linux

This shows you how to set up Kali Linux in Docker in some basics steps:

# get the image and run the container
$ docker pull kalilinux/kali-linux-docker
$ docker images
$ docker container run -t -d kalilinux/kali-linux-docker
$ docker ps
# get the label under ’Names’ from the previous command
$ docker exec -it /bin/bash
# this is executed inside the container
$ apt-get update
$ apt-get upgrade
# I use 'nikto' as an example
$ apt-get install nikto
$ nikto -Version
# the same command but executed outside the container
$ docker container exec -it nikto -Version