Building web apps that run the same in multiple environments can be a time-consuming process. This work session we will learn how to use Docker and how you can efficiently build apps that run consistently across any machine. First, you'll learn about Docker, docker hub, Images, Container and commands. Next, you'll learn how to work with images, as well as Docker containers After that, you'll discover how to get a fully-functional development environment up and running. By the end of this session, you'll be able create lightweight apps that run the same no matter the environment.
##Installation on Linux
- Install
curl
sudo apt-get install curl
- Quick and easy
dockerInstall byscript:
curl -sSL https://get.docker.com/ | sh
- Add your user to
dockergroup.
sudo usermod -aG docker $USER
- Start the
dockerdaemon.
sudo service docker start
-
Log out or restart your pc to ensures your user is running with the correct permissions.
-
Verify your work by running
dockerwithoutsudo.
docker run hello-world
#####Install Docker Composer
docker composeinstall permission.
sudo chown -R $(whoami) /usr/local/bin
- Install
docker compose
curl -L https://github.com/docker/compose/releases/download/1.8.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
docker composeexecutable permissions.
chmod +x /usr/local/bin/docker-compose
- Test the installation.
docker-compose --version
#####Optional
- Install Oh My Zsh with the Docker, git plugin for autocompletion of docker and git commands.
-
docker pullpulls an image from docker registry to local machine.Usage:
docker pull [OPTIONS] ImageName:TAGExample:
docker pull nginx:1.11.9-alpine -
docker runcreates and starts a container in one operation.Usage:
docker run [OPTIONS] ImageName:TAG [COMMAND]Example:
docker run -it --name nginx -p 80:80 -v /some/content:/usr/share/nginx/html:ro nginx:1.11.9-alpine -
docker startstarts a container so it is running.Usage:
docker start [OPTIONS] ContainerName or ContainerID [CONTAINER...] -
docker stopstops a running container.Usage:
docker stop [OPTIONS] ContainerName or ContainerID [CONTAINER...] -
docker restartstarts a container so it is running.Usage:
docker restart [OPTIONS] ContainerName or ContainerID [CONTAINER...]
-
docker buildcreates image from Dockerfile.Usage:
docker build [OPTIONS] PATH | URL | -Example:
Docker build -t YourImageName:tag . or Dockerfile location -
docker execRun a command in a running container.Usage:
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]Example:
Docker exec -it nginx bash or sh -
docker imagesshows all images. -
docker psshows running containers. -
docker ps -ashows running and stopped containers. -
docker renameallows the container to be renamed.Usage:
docker rename OldContainerName NewContainerName -
docker rmRemove one or more containers.Usage:
docker rm [OPTIONS] ContainerName or ID [CONTAINER...]Example:
Docker rm -f nginxForce the removal of a running container -
docker rmiRemove one or more images.Usage:
docker rmi [OPTIONS] ImageName or ID [IMAGE...]Example:
Docker rmi -f nginxForce removal of the image.
docker logsgets logs from container.
Usage:docker logs [OPTIONS] ContainerID or ContainerName
-
docker inspectlooks at all the info on a container (including IP address)Usage:
docker inspect [OPTIONS] ContainerID or ContainerName -
docker portshows public facing port of container.
Usage:docker port [OPTIONS] ContainerID or ContainerName
docker topshows running processes in container.
Usage:docker top [OPTIONS] ContainerID or ContainerName
docker statsshows containers' resource usage statistics.
Usage:docker stats [OPTIONS] ContainerID or ContainerName
docker diffshows changed files in the container's FS.
Usage:docker diff [OPTIONS] ContainerID or ContainerName
FROMSets the Base Image for subsequent instructions.
Usage: FROM <image>:<tag>
MAINTAINERSet the Author field of the generated images.
Usage: MAINTAINER <name>
RUNexecute any commands in a new layer on top of the current image and commit the results.
Usage: RUN <command>
CMDprovide defaults for an executing container.
Usage: CMD ["executable","param1","param2"]
EXPOSEinforms Docker that the container listens on the specified network ports at runtime. NOTE: does not actually make ports accessible.
Usage: EXPOSE <port> [<port>...]
ENVsets environment variable.
Usage: ENV <key> <value> or ENV <key>=<value>
ADDcopies new files, directories or remote file to container. Invalidates caches.
Usage: ADD <src>... <dest>
COPYcopies new files or directories to container.
Usage: COPY <src>... <dest>
ENTRYPOINTconfigures a container that will run as an executable.
Usage: ENTRYPOINT ["executable", "param1", "param2"]
VOLUMEcreates a mount point for externally mounted volumes or other containers.
Usage: VOLUME ["/data"]
USERsets the user name for following RUN / CMD / ENTRYPOINT commands.
Usage: USER <userName>
WORKDIRsets the working directory.
Usage: WORKDIR /path/to/workdir
ARGdefines a build-time variable.
Usage: ARG <name>[=<default value>]