First, clone a repository
The Getting Started project is a simple GitHub repository which contains everything you need to build an image and run it as a container.
Clone the repository by running Git in a container.
docker run --name repo alpine/git clone https://github.com/docker/getting-started.git
docker cp repo:/git/getting-started/ .
Container identification
Name (--name
)🔗
The UUID identifiers come from the Docker daemon.
If you do not assign a container name with the --name
option, then the daemon generates a random string name for you. Defining a name
can be a handy way to add meaning to a container.
If you specify a name
, you can use it when referencing the container within a Docker network. This works for both background and foreground Docker containers.
docker cp container_name:<path> <local_path>
to copy the container files to host.
Now, build the image - BUILD
A Docker image is a private file system just for your container. It provides all the files and code your container needs.
cd getting-starteddocker build -t docker101tutorial .
Finally, the -t
flag tags our image.
Think of this simply as a human-readable name for the final image. Since we named the image docker101tutorial
, we can refer to that image when we run a container.
Run your first container - RUN
Start a container based on the image you built in the previous step. Running a container launches your application with private resources, securely isolated from the rest of your machine.
docker run -d -p 80:80 --name docker-tutorial docker101tutorial
You'll notice a few flags being used. Here's some more info on them:
-d
- run the container in detached mode (in the background)-p 80:80
- map port 80 of the host to port 80 in the containerdocker101tutorial
- tag of the image to use (name we declared in the previous steps)--name docker-tutorial
Container identification rather than a random string name.
Now save and share your image - STORE
Save and share your image on Docker Hub to enable other users to easily download and run the image on any destination machine.
docker tag docker101tutorial vijayanandrp/docker101tutorialdocker push vijayanandrp/docker101tutorial
What is a container?
Now that you've successfully run a container, let's ask ourselves what is a container? Simply put, a container is another process on your machine that has been isolated from all other processes on the host machine. That isolation leverages kernel namespaces and cgroups, features that have been in Linux for a long time. Docker has worked to make these capabilities approachable and easy to use.
What is a container image?
When running a container, it uses an isolated filesystem. This custom filesystem is provided by a container image
. Since the image contains the container's filesystem, it must include everything needed to run the application - all dependencies, configuration, scripts, binaries, etc. The image also contains other configuration for the container, such as environment variables, a default command to run, and other metadata.
If you're familiar with
chroot
, think of a container as an extended version ofchroot
. The filesystem is simply coming from the image whereas a container adds additional isolation that is not available when simply usingchroot
.
Dockerfile
# Install the base requirements for the app.# This stage is to support development.FROM python:alpine AS baseWORKDIR /appCOPY requirements.txt .RUN pip install -r requirements.txt
FROM node:18-alpine AS app-baseWORKDIR /appCOPY app/package.json app/yarn.lock ./COPY app/spec ./specCOPY app/src ./src
# Run tests to validate appFROM app-base AS testRUN yarn installRUN yarn test
# Clear out the node_modules and create the zipFROM app-base AS app-zip-creatorCOPY --from=test /app/package.json /app/yarn.lock ./COPY app/spec ./specCOPY app/src ./srcRUN apk add zip && \ zip -r /app.zip /app
# Dev-ready container - actual files will be mounted inFROM base AS devCMD ["mkdocs", "serve", "-a", "0.0.0.0:8000"]
# Do the actual build of the mkdocs siteFROM base AS buildCOPY . .RUN mkdocs build
# Extract the static content from the build# and use a nginx image to serve the contentFROM nginx:alpineCOPY --from=app-zip-creator /app.zip /usr/share/nginx/html/assets/app.zipCOPY --from=build /app/site /usr/share/nginx/html
docker-compose.yml
version: "3.7"
services: docs: build: context: . dockerfile: Dockerfile target: dev ports: - 8000:8000 volumes: - ./:/app
docker-compose.yml Another example
services: app: image: node:18-alpine command: sh -c "yarn install && yarn run dev" ports: - 3000:3000 working_dir: /app volumes: - ./:/app environment: MYSQL_HOST: mysql MYSQL_USER: root MYSQL_PASSWORD: secret MYSQL_DB: todos
mysql: image: mysql:8.0 volumes: - todo-mysql-data:/var/lib/mysql environment: MYSQL_ROOT_PASSWORD: secret MYSQL_DATABASE: todos
volumes: todo-mysql-data:
docker compose up -d
docker compose logs -f
docker compose down
#mythoughts