Skip to content

Latest commit

 

History

History
196 lines (138 loc) · 5.42 KB

README.md

File metadata and controls

196 lines (138 loc) · 5.42 KB

Workshop » container basics

This set of tasks covers the basic operations on containers with Docker. Here you will learn:

After you have installed docker, open your favourite command line terminal (Powershell, iTerm, Terminal, ...) and execute the following command.

How to check docker status

docker ps --all

The previous command will show all the running containers on the host OS. Some of them might be stopped, running or dead. This is one of the basic docker commands to check the current status of containers within your operating system. If there are no stopped or running containers, the list will be empty.

Show demo

asciicast

Run a simple container

docker run -it --name ubuntu-container ubuntu:latest

This will download an docker image with latest Ubuntu linux distribution, start it and open an interactive terminal session. After that you will have an interactive session within Ubuntu Linux operating system. When you are done with your work, you can exit the container by executing exit command.

The container will go into stopped status (you can check the status of containers with docker ps --all as discussed in this section.

By running the command below you can inspect the container and get the detailed information about the running or stopped container.

docker inspect ubuntu-container
Show demo

asciicast

Expose a local container network port

Container can have network services (web servers, mail servers, ...) running inside them.

The following command will start a container, expose its TCP network port 80 to localhost port 8080 (-p 127.0.0.1:8080:80), put it in the background (--detach) and named it (--name k8s-workshop-container).

You can access the container with your browser here.

docker run \
    -p 127.0.0.1:8080:80 \
    --detach \
    --name k8s-workshop-hello \
    eu.gcr.io/k8s-workshop-274312/k8s-workshop:latest

The container image was prepared in advance and pushed into Google cloud repository.

The container is now running in the background and acting as an HTTP server, listening on localhost:8080.

You can check the logs of the container by executing:

docker logs k8s-workshop-hello

Now the container is running in the background (check status with docker ps --all).

After you are done, stop it with the following command.

docker stop k8s-workshop-hello

How to use environment variables

You can pass environment variables to running container by specifying them in command line (--env). In the example below, the variable CODE is used by the container and displayed when you open the browser on this address.

docker run \
    -p 127.0.0.1:8080:80 \
    --detach \
    --name k8s-workshop-env \
    --env CODE=k8s-workshop \
    eu.gcr.io/k8s-workshop-274312/k8s-workshop:latest

After you are done, stop it with the following command.

docker stop k8s-workshop-env
Show demo

asciicast

Expose a local directory to container

Docker provides a capability to expose (mount) a file or directory to container. In the example below, we will mount a local directory to a specific directory in the container. This will be used in the http server of the container.

docker run \
    -p 127.0.0.1:8080:80 \
    --detach \
    --name k8s-workshop-directory \
    --mount type=bind,source=/Users/k8s-workshop/,target=/tmp \
    eu.gcr.io/k8s-workshop-274312/k8s-workshop:latest

After you are done, stop it with the following command.

docker stop k8s-workshop-directory

Various docker commands

Remove stopped containers

docker rm ubuntu-container

Docker images

Each container image is stored on the disk and can be reused.

docker image ls

You can delete the container image by executing the following command. (e.g. ubuntu:latest). You can only remove a container image, which is currently not in use.

docker image rm ubuntu:latest

Docker system cleanups

docker system prune

Note: The command may also remove other container images that are not related to the workshop. For more details consult the manual.

Show demo

asciicast