Install docker from https://docs.docker.com/engine/installation/
Run the docker for Mac app. This must be started before you can call docker commands.
Docker containers are like a lightweight virtual machine.
They can be created from scratch or downloaded from the docker hub https://hub.docker.com/
An instance of an image is called a container. You have an image, which is a set of layers. If you start this image, you have a running container of this image. You can have many running containers of the same image. You can stop a container and you can run a stopped container and keep on doing things you where doing with it.
You can see all your images with docker images whereas you can see your running containers with docker ps (and you can see all containers with docker ps -a).
So a running instance of an image is a container.
Use docker pull container_name
to download a container.
Note: Some containers have multiple tags that are used to identify different versions. You may need to append the tag name onto the end of the container name e.g
docker pull container_name:1.2
To view all the docker images on the system
docker images -a
To delete a docker image
docker rmi Image Image
To view that status of any docker containers use:
docker ps
Docker containers are seperated from the OS. They have to be started and then logged into.
Docker containers will not save there state so if they are stopped and restarted all files that where saved in them are lost.
Mount a directory and save stuff in it to get around this issue. Alternatively you can save the state of a container with
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c3f279d17e0a ubuntu:12.04 /bin/bash 7 days ago Up 25 hours desperate_dubinsky
197387f1b436 ubuntu:12.04 /bin/bash 7 days ago Up 25 hours focused_hamilton
$ docker commit c3f279d17e0a svendowideit/testimage:version3
Note that this will create a new image, which will take a few GBs space on your disk.
If my docker container is called gcr.io/tensorflow/tensorflow:latest-devel then start it with
docker run -it gcr.io/tensorflow/tensorflow:latest-devel
Here, ‘-it’ will allocate a terminal to created container.
You can log into the Docker Image using the root user (ID = 0) instead of the provided default user when you use the -u option. This will grant you sudo privleges.
docker run -u 0 -it mycontainer bash
To mount a folder so it can be accessed by both the OS and the docker container start docker container with the following command:
docker run -it -v $HOME/directory/:d_dir/ gcr.io/tensorflow/tensorflow:latest-devel
This starts our docker container and also mounts a directory from the OS named directory and
calls it dir in the container. This directory is now accessible from both the docker container and the OS.
To install a custom package or modify an existing docker image we need to
- run a docker a container from the image we wish to modify
- modify the docker container
- commit the changes to the container as a docker image
- test changes made to image
The command to do this is,
docker run -it yhat/scienceops-python:0.0.2 /bin/bash
-
The -i tells docker to attach stdin to the container
-
The -t tells docker to give us a pseudo-terminal
-
/bin/bash will run a terminal process in your container
Once we are in our container we can install package(s), and set environment variables
$ sudo apt-get install vim
$ export AWS_SECRET_KEY=mysecretkey123
$ export AWS_ACCESS_KEY=fooKey
Copy files/folders to the container or vice versa.
docker cp source_dir/. container_instance_name:/destination_dir/
Use the following to get the container instance name
docker ps --format "{{.Names}}"
When you are done modifying your container you must exit by running the
exit
command. Once we exit the container, we need to find the
container ID by running
docker ps -a
Copy the container ID for the container you just modified, and then run
the docker commit
command to commit changes to your container as an image.
docker commit [options] [container ID] [repository:tag]
An example docker commit
command is the following.
docker commit e8f0671518a2 yhat/scienceops-python:0.0.2
Note here! You must commit the changes with the same tags as the scienceops image on your system. To see your new image run.
docker images
To test your changes when adding an environment variable run the test command
$ docker run -it yhat/scienceops-python:0.0.2 echo $AWS_SECRET_KEY
Create an empty directory. Change directories (cd) into the new directory.
Create a file called Dockerfile with no name extension.
Copy-and-paste the following content into that file, and save it. Take note of the comments that explain each statement in your new Dockerfile.
# Use an official Python runtime as a parent image
FROM python:2.7-slim
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app
# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
This Dockerfile refers to a couple of files we haven’t created yet, namely app.py and requirements.txt. Let’s create those next.
From inside the my_build directory (where the dockerfile is located), we’ll use the docker build command, passing the -t flag to “tag” the new image with a name, which in this case will be my_image. The . indicates that the Dockerfile is in the current directory, along with so-called “context” — that is, the rest of the files that may be in that location:
cd ~/my_build
docker build -t my_image .
Now once built we can use the following to view it along with all our other docker images.
docker images
- Create an empty directory and copy or create script.sh in it
- Create Dockerfile name it "Dockerfile" with following content:
FROM repo/image
WORKDIR $HOME
Where repo/image is the name of the downloaded image you want to base the new image on.
- Run the following command from the new directory.
docker build -t="new_image_name_with_tag" .
Docker commands