Docker notes
Basic Concepts
An image is a lightweight, stand-alone, executable package that includes everything needed to run a piece of software, including the code, a runtime, libraries, environment variables, and config files. Docker Hub: A registry of Docker images. You can think of the registry as a directory of all available Docker images. Since the image doesn't exist locally, the client will first fetch the image from the registry and then run the image. Container: A runtime instance of an image. This is a image becomes in memory when actually executed. Dockerfile: A simple text-file that contains a list of commands that the Docker client calls while creating an image. It's a simple way to automate the image creation process. In an image, a layer is modification to the image, represented by an instruction in the Dockerfile. Layers are applied in sequence to the base image to create the final image. When an image is updated or rebuilt, only layers that change need to be updated, and unchanged layers are cached locally.
Dockerfile Commands
Command | Description |
---|---|
FROM | Load from a base image |
CMD | Cmd takes the following format CMD [“excutable”, ”params1”, ”params2”] |
RUN | Run is used to execute any commands. |
COPY | Copy the folder/ files from the local host source to the destination in the container. |
ENV | Set the environment variables. |
USER | Set the (UID) username which is to run the container based on the image being built. |
VOLUME | Enable access from your container to a directory on the host machine. |
Docker Commands
Command | Description |
---|---|
docker run <image> | run a docker container based on the image |
docker ps | show all containers that are currently running |
docker ps -a | show a list of all containers that we ran |
docker history <image> | view all the layers that make up the image once the image is built |
docker exec -it <container> /bin/bash | launch the docker from other terminals |
docker container commit <container> <repository[:TAG]> | create a new image from a container’s changes |
docker container kill <container> | kill one or more running containers |
docker container rm <container> | remove existing container |
docker image ls | list image built on the machine |
docker image rm <container> | remove image |
nvidia-docker build -t <container> -f <Dockerfile> nvidia-docker run -it –rm automatically remove the container when it exits. –volume list bind mount a volume –env list set environment variables -u user or UID (i.e. mavs) –privileged give extended privileges to this container
Docker examples:
Stopping a running container:
First look the containers:
docker container ls
then stop the container that is running for instance:
docker rm -f mavs_container
Save changes made in docker container:
- Find the container ID
$docker ps -l
- Commit the change
$docker commit <container_id> [repository[:tag]]`
- Change the docker name tag in ./run.sh
Upload base image to Docker Hub:
- Tag the image
$docker tag <container> [DockerUserName/Repository[:tag]]
- Push the image to Docker Hub repository
$docker push [DockerUserName/Repository[:tag]]
Open up new terminals to access Container (example):
- Look at the current containers available with
$docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ce4096e14bef mavs "/ros_entrypoint.sh …" 34 minutes ago Up 34 minutes vigilant_johnson
- Open a new terminal
$docker exec -it vigilant_johnson /bin/bash
Note: you can use “tab” key to auto-complete the <container_name>
Docker hub
How to update base image on docker hub
- Run
$sh build.sh
- login to docker
$docker login
- ensure the name of the image
$docker image ls
Currently this should be avpg_base, where the image name is specified in the build.sh
script
- tag the image
$docker tag avpg_base avpg/cain:base_cudagl
assuming that the image name is avpg_base
- publish the image
$docker push avpg/cain:base_cudagl
see the docker tutorial on containers for more detailed instructions
Pull and run the image from the remote repository
It would be interesting to try this with the higher-level image, but this is not yet tested.
docker run -p 4000:80 avpg/cain:base_cudagl
Running out of space?
Check your current images:
docker image ls
Check your current containers:
docker image ls
Do some trimming
docker image prune
docker container prune
docker system prune
What Are Volumes?
For simplicity, volumes are the mechanism for persisting data generated by and used by Docker containers. Click here for more information. In $run.sh$, several volumes are created. The results/ directory is used to store the demonstration data and we can do some post process after we terminate the container. As for ros/src/ directory, it shares the source files in the local machine with container. That is, we can modify the files outside the container and see the instant change inside the container. And it fits the scenario where we need to update script file, such as Python, ROS launch file, without rebuild the image.