Created
September 8, 2018 23:31
-
-
Save dturnergs/7961a91f5187fdee9e36b039fdb0e2d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Search for Images from Docker Hub | |
docker search ubuntu | |
Run an Images from Docker Hub | |
docker run -d ubuntu (note: the "-d" switch will run it in the background) | |
List running containers | |
docker ps | |
Show all of a containers configuration details | |
docker ps | |
docker inspect {container-id} | docker inspect {friendly-name} | |
Show logs of a container | |
docker ps | |
docker logs {container-id} | docker logs {friendly-name} | |
Give a container a name and a port to access it | |
docker run -d --name ubuntuTest -p 6379:6379 ubuntu:latest | |
By default, the port on the host is mapped to 0.0.0.0, which means all IP addresses. You can specify a particular IP address when you define the port mapping, for example, -p 127.0.0.1:6379:6379 | |
Dynamic Port Assignment | |
docker run -d --name ubuntuDynamic -p 6379 ubuntu:latest | |
docker port ubuntuDynamic 6379 | |
Persisting Data | |
Containers are designed to be stateless. Binding directories (also known as volumes) is done using the option -v <host-dir>:<container-dir>. When a directory is mounted, the files which exist in that directory on the host can be accessed by the container and any data changed/written to the directory inside the container will be stored on the host. This allows you to upgrade or change containers without losing your data. | |
docker run -d --name ubuntuMapped -v /opt/docker/data/redis:/data ubuntu | |
TIP: Docker allows you to use $PWD as a placeholder for the current directory. | |
Running A Container in the Foreground | |
Running a container using the "-d" switch puts the container in a detached state to run an application in the background. Enabling a foreground process such as ps or bash requires the interactive switch, for example to access a bash shell the "-it" switch is required. | |
docker run ubuntu ps | |
docker run -it ubuntu bash | |
Build a Docker Image | |
Create a file called Dockerfile | |
Create a simple index.html file | |
Paste the following code: | |
FROM nginx:alpine | |
COPY . /usr/share/nginx/html | |
Task | |
docker build -t webserver-image:v1 . | |
(don't miss the "." at the end) | |
The newly created image can be viewed using the command: | |
docker images | |
The built image will have the name: webserver-image with the tag v1 | |
Run | |
Run the newly built image providing a friendly name and tag. As it's a web server, bind port 80 to the host using the "-p" parameter | |
docker run -d -p 80:80 webserver-image:v1 | |
Once started the results of port 80 can be accessed via the command: | |
curl docker | |
Building Container Images | |
Base Image | |
The first line of the Dockerfile should be: | |
FROM nginx:1.11-alpine | |
NOTE: always use a particular base image version as opposed to the easy way of selecting "Latest". Doing so could prevent one from taking on an unexpected version. | |
Configure the base image using RUN and COPY | |
RUN allows execution of any command as would be at a command line. An example would be install different application packages or running a build command. The results of the RUN are persisted to the image so it's important not o leave any unnecessary or temp files on the disk as these will be included in the image. | |
COPY <src> <dest> allows copying files from the directory containing the Dockerfile to the container's image. Useful for source code and assets that are needed to be deployed inside the container. | |
EXPOSE <port> command tells Docker which port should be open and can be bound too. Multiple ports can be defined on a single command, i.e., EXPOSE 80 433 or EXPOSE 7000-8000 | |
Dockerfile | |
FROM nginx:1.11-alpine | |
COPY ./index.html /usr/share/nginx/html | |
EXPOSE 80 | |
CMD ["nginx", "-g", "daemon off;"] | |
With the image configured and ports defined, the commands to launch the application need to be defined. | |
["cmd", "-a", "arga value","-b","argb-value"] | |
example of a command array as shown above: cmd -a arga value -b argb-value | |
The command to run NGINX is: | |
nginx -g daemon off; | |
An alternative approach to CMD is ENTRYPOINT. While a CMD can be overridden when the container starts, a ENTRYPOINT defines a command which can have arguments passed to it when the container launches. | |
In this example, NGINX would be the entrypoint with -g daemon off; the default command. | |
CMD ["nginx", "-g", "daemon off;"] | |
After the Dockerfile is complete, docker build command is required to turn the file into an image. | |
docker build . | |
or | |
docker build -t nginx-image:latest . | |
Launching New Image | |
Use the friendly name assigned to the image created to launch it. NGINX is designed to run as a background service so you should include the option -d. To make the web server accessible, bind it to port 80 using p 80:80 | |
docker run -d -p 80:80 <image-id|friendly-tag-name> | |
The launched web server can be accessed via the hostname docker. After the container is launched run the command: | |
curl -i http://docker | |
This will return the index file via NGINX and the image built. | |
Using the command "docker ps" will show if the container is running. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment