Docker Basics

The below docker commands should give you a basic understanding of how docker works.  This is obviously not a comprehensive lesson, but should give you enough information to take off on your own.

Create an account

https://hub.docker.com

Run a basic container

Run in detached mode on internal port 80, external port 5001 using the httpd:2.4 image on docker hub, and name the container itsmetommy-httpd.

docker run -d --name itsmetommy-httpd -p 5001:80 httpd:2.4
  • -name <container_name>
  • -d detached mode
  • -p <external_port>:<internal_port>
  • <image_name>:<version> (e.g. httpd:2.4)

View your URL

http://localhost:5001

View your images

docker images | grep httpd

View your running docker containers

docker ps

View your running and stopped docker containers

docker ps -a

Run centos in interactive and tty mode

docker run -it --name itsmetommy-centos centos

Then exit out of the container.

exit
  • -it instructs Docker to allocate a pseudo-TTY connected to the container’s stdin; creating an interactive bash shell in the container
  • exit out of the container

View your running docker containers

docker ps

View your running and stopped docker containers

Notice that itsmetommy-centos is stopped. This is due to you not running in detached mode.

docker ps -a

Start itsmetommy/centos back up

docker start itsmetommy-centos

View your running docker containers

Notice that itsmetommy/centos is now running.

docker ps

Lets connect to itsmetommy/centos

docker attach itsmetommy-centos

If you type ‘exit’ you will by design stop the container.

This is where pressing CTRL P + Q comes in handy.

CTRL P + Q

You can view that itsmetommy-centos is still running.

docker ps

Stop the docker containers

docker stop itsmetommy-httpd;docker stop itsmetommy-centos

View your running and stopped docker containers

docker ps -a

Remove the docker containers

docker rm itsmetommy-httpd;docker rm itsmetommy-centos

Create a project directory

mkdir learn-docker;cd learn-docker

Create a public-html directory with index.html

mkdir public-html;echo "Hello World" > public-html/index.html

Create a Dockerfile

What if we want to record our changes and build our own image? Dockerfiles to the rescue!

This will download the httpd image from docker hub and copy the contents from public-html into /usr/local/apache2/htdocs within the httpd container.

vi Dockerfile
FROM httpd:2.4
COPY ./public-html/ /usr/local/apache2/htdocs/
EXPOSE 80

Build

This will build a new image called itsmetommy/learn-docker based on the Dockerfile.

docker build -t itsmetommy/learn-docker .
  • -t tag

Push

https://hub.docker.com/

Make sure you are pointing to localhost and logged into docker hub.

Unset environment to localhost.

eval $(docker-machine env --unset)

View your environment.

env | grep DOCKER

If you haven’t already, login using your Docker Hub account.

docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: itsmetommy
Password:
Login Succeeded

Now push your new image to your docker hub.

docker push itsmetommy/learn-docker

https://hub.docker.com/u/itsmetommy/

Volumes

Volumes give a running container the ability to see files on the host machines file system. They DO NOT change the image.

If you ever wanted to make any changes to lets say index.html, it would be a pain to have to re-build the image every time you test a change. This is where mounting your local volume within the container comes into play.

Lets run the container again except with a volume mount.

Note: You must put the full path (not relative).

docker run -d -p 5001:80 -v /Users/$USER/learn-docker/public-html:/usr/local/apache2/htdocs/ --name itsmetommy-learn-docker itsmetommy/learn-docker

IMPORTANT: If you update any files, you will need to re-build the image to get an updated copy of the image.

Test

http://localhost:5001

Now update your local index.html file.

echo "Hello $USER" > public-html/index.html

http://localhost:5001 # refresh browser

Awesome right? 🙂

Clean up — Stop/remove/delete the container

docker stop itsmetommy-learn-docker;docker rm itsmetommy-learn-docker;docker rmi itsmetommy/learn-docker

Doing a bunch of docker run commands can be a huge pain.

That’s where docker-compose saves the day.

Docker-compose

Create a learn-docker-compose folder and cd into it

mkdir learn-docker-compose; cd learn-docker-compose

Create the httpd and nginx folders and files

mkdir -p httpd/public-html nginx/public-html; echo "Hello HTTPD" > httpd/public-html/index.html; echo "Hello NGINX" > nginx/public-html/index.html

Create docker-compose.yml

vi docker-compose.yml
version: '3'

services:
  apache2:
    build: ./httpd
    image: httpd
    volumes:
      - ./httpd/public-html:/usr/local/apache2/htdocs
    ports:
      - 5001:80
    container_name: itsmetommy-httpd
  nginx:
    build: ./nginx
    image: nginx
    volumes:
      - ./nginx/public-html:/usr/share/nginx/html
    ports:
      - 5002:80
    container_name: itsmetommy-nginx

Run docker-compose up

docker-compose up -d
  • -d detached mode

View your sites

http://localhost:5001
http://localhost:5002

Update index.html and refresh browsers

Update the httpd and nginx index.html file.

echo "Hello HTTPD 2.0" > httpd/public-html/index.html
echo "Hello NGINX 2.0" > nginx/public-html/index.html

http://localhost:5001 # refresh
http://localhost:5002 # refresh

List containers

docker-compose ps

Run docker-compose stop

This stops the containers.

docker-compose stop

Run docker-compose kill

If the containers won’t stop, you can issue a kill.

docker-compose kill

Finally, remove the containers

docker-compose rm