Skip to content

Instantly share code, notes, and snippets.

@kranstrom
Last active February 17, 2017 16:32
Show Gist options
  • Save kranstrom/13f507800c4dfd1203f9ae55a083b9c5 to your computer and use it in GitHub Desktop.
Save kranstrom/13f507800c4dfd1203f9ae55a083b9c5 to your computer and use it in GitHub Desktop.
Docker Training Notes

Docker Training Notes

Sending on some notes. Sometimes it's difficult to keep notes if things go fast...

What did you do in this first training?

Built your first Dockerfile

The default Dockerfile is named Dockerfile (you can name it something different, but then have to be explicit when you build the image). You created this file using touch Dockerfile. We then used your text editor to edit.

The Dockerfile are build instructions for a container image. We can share this image with people, servers, CI processes, etc.

The Dockerfile expects a FROM - that is the image that you want to use as your base. In this case you used tomcat:jre8.

We also added Rick as MAINTAINER (person/group to blame when things go wrong).

Finally, we explored the ENV tag allowing us to set environment variables. This is a common use for passing parameters into a container. ENV CATALINA_OPT "-Xmx6044M" would set the CATALINA_OPT environment variable as -Xmx6044M.

Thus, your base image would read something like this:

FROM tomcat:jre8
MAINTAINER "Rick"
ENV CATALINA_OPT "-Xmx6044M"

To build this image we used the following syntax from within the directory where the Dockerfile was stored: docker build -t="dockercm" .

The -t indicates the name of the image.

Explored Docker Hub and understanding tags

Docker Hub is a public container registry, meaning anyone can go to Docker Hub and share/pull pre-built container images. These images are all ready to use.

Be aware that you want to look for sound images to use. You can see this by number of people that have starred a certain registry repo (tomcat is an example of a Dcoker Hub registry repo) or by the number of people that have pulled from a particular registry reop.

We also started understanding tags. If no tag is defined when pulling from a registry it will assume the latest tag. Thus, docker pull tomcat is the equivalent of docker pull tomcat:latest.

Since latest is a default tag, it also means docker build -t="dockercm" . is the same as docker build -t="dockercm:latest" .

Run Docker containers

Indeed, building a virtual image is just part of the process, but we also need to run the container.

To run we used: docker run -it dockercm

In the simple above example you would run your built dockercm container in interactive mode. We can apply the -d which will run disconnected, meaning you won't enter into the container or follow the container logs.

We also went through applying a port mapping to the host by issuing -p. So -p="8085:8080" means port 8080 in the container is mapped to port 8085 on the host.

More so we went through volume-mounting a file into the container as such: -v="C:\somedir\ab.war:/usr/local/tomcat/webapps/ab.war. We noted that file is mapped live to the container, meaning any modification to the file on the host will be applied to the container and vice versa.

Also, whatever is applied after dockercm means going into that entrypoint and executing that command directly. Thus, we did run: docker run -it dockercm bash which allowed us to immediately jump into bash within the container.

Lastly, we noted that docker run can be used directly without first pulling the image. If the image doesn't exist locally it will attempt pulling it from the registry.

So you could run: docker exec -it -d tomcat:jre7

Since you don't have the tag above on your computer, it would go out and find it on Docker Hub in this case.

Jumping into an already started container

If your container is already started, we can jump into bash in that container using docker exec as such: docker exec -it <image-name> bash

How do you get the image name? Well, Docker works very similar to bash in this regard (you'll find more on that if we do additional sessions or you read more). So docker ps works similar to just issuing ps in bash but from a docker perspective. It lists out the processes (running containers) and information about them.

I think this sums up most of what was covered in our 1h training. Hopefully you find it helpful and the above notes serve as a good refresher.

Happy Dockering.

Thanks,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment