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.

Install

https://docs.docker.com/get-docker

Create docker hub account

https://hub.docker.com

Login to docker hub

docker login

Search for images

docker search ubuntu

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

View running docker containers

docker ps

View running and stopped docker containers

docker ps -a

Run in interactive and tty mode

docker run -it --name itsmetommy-ubuntu ubuntu

Exit 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-ubuntu is stopped. This is due to you not running in detached mode.

docker ps -a

Start the container.

docker start itsmetommy-ubuntu

View your running docker containers

Notice that the container is now running.

docker ps

Lets connect to itsmetommy/ubuntu

docker attach itsmetommy-ubuntu
  • If you type ‘exit’ you will stop the container
  • Pressing CTRL P + Q will exit and keep the container running
CTRL P + Q

You can see that itsmetommy-ubuntu is still running.

docker ps

Stop the docker containers

docker stop itsmetommy-httpd; docker stop itsmetommy-ubuntu

View your running and stopped docker containers

docker ps -a

Remove the docker containers

docker rm itsmetommy-httpd; docker rm itsmetommy-ubuntu

Clean up images

docker image rm httpd:2.4; docker image rm ubuntu

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

Create a repository on docker hub called learn-docker.

https://hub.docker.com/

Now push your new image to your docker hub repository.

Important: Substitute itsmetommy with your docker hub name. 

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 Tommy" > 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

Change back a directory.

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

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

Docker-compose: Create docker-compose.yml

vi docker-compose.yml
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

Docker-compose: Run docker-compose up

docker-compose up -d
  • -d detached mode

Docker-compose: View your sites

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

Docker-compose: 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

Docker-compose: List containers

docker-compose ps

Docker-compose: Run docker-compose stop

This stops the containers.

docker-compose stop

Docker-compose: Run docker-compose kill

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

docker-compose kill

Docker-compose: Finally, remove the containers

docker-compose rm
, ,