Hot Posts

hot/hot-posts

Docker Basics



Docker is a platform for developing, shipping, and running applications in containers. Containers are lightweight, portable, and self-sufficient environments that allow developers to package an application with all its dependencies, making it easy to deploy and run on any system.

Here are the basic concepts and commands you need to know to get started with Docker:

  1. Docker Images: A Docker image is a pre-built package that contains all the files and dependencies needed to run an application. You can download images from a public or private registry, such as Docker Hub, or create your own images using a Dockerfile.

  2. Docker Containers: A Docker container is a running instance of an image. You can start, stop, and manage containers using the docker run, docker start, docker stop, and docker rm commands.

  3. Dockerfile: A Dockerfile is a script that contains instructions for building an image. It specifies the base image, the application code, and any dependencies that need to be installed.

  4. Docker Volumes: A Docker volume is a way to store data outside of a container's filesystem. This allows data to persist even if the container is deleted, and can also be used to share data between containers.

  5. Docker Compose: Docker Compose is a tool for defining and running multi-container applications. It uses a docker-compose.yml file to define the services, networks, and volumes of an application.

  6. Docker Networking: Docker allows you to create virtual networks to connect containers. You can use the docker network create and docker network connect commands to create and connect to networks, and the docker network inspect command to view network information..

Here are some basic commands to get you started:

  • docker run: Runs a command in a new container
  • docker start: Starts one or more stopped containers
  • docker stop: Stops one or more running containers
  • docker rm: Removes one or more containers
  • docker images: Lists all images on the local system
  • docker build: Build an image from a Dockerfile
  • docker pull: Pull an image from a registry
  • docker push: Push an image to a registry
  • docker-compose up: Start and run the services defined in a docker-compose.yml file
  • docker-compose down: Stop and remove the services defined in a docker-compose.yml file
Docker Volumes: As mentioned before, volumes allow you to store data outside of a container's filesystem, so it persists even if the container is deleted. You can use the docker volume create and docker volume inspect commands to create and view volumes, and the -v flag with the docker run command to mount a volume to a container.

Docker Port Mapping: By default, Docker containers are isolated from the host network. To access a container's network services, you need to map the container's ports to the host's ports. You can use the -p flag with the docker run command to specify the port mapping.

Docker Logs: To view the logs of a running container, you can use the docker logs command. This is helpful for debugging and troubleshooting issues with your container.

Docker Tags: You can use tags to give a version number to your images. The tag name is appended to the image name, e.g. image-name:tag-name.

Docker Registry: As mentioned before, you can pull and push images from a registry. The most popular registry is Docker Hub, but you can also use other public or private registries.

Docker Compose: This is a tool for defining and running multi-container applications. You can use a docker-compose.yml file to define the services, networks, and volumes of an application, and then use the docker-compose up and docker-compose down commands to start and stop the application

Dockerizing a Spring Boot application 

It involves packaging the application and its dependencies into a Docker image, so it can be easily deployed and run in a container. Here's a general approach for doing this:

Create a Dockerfile: This is a script that contains instructions for building the Docker image. It specifies the base image, the application code, and any dependencies that need to be installed. The Dockerfile should include a command to copy the Spring Boot executable jar into the container and configure the entrypoint to run the jar file.

Build the image: Use the docker build command to build the image from the Dockerfile. This will create a new image that contains your application and all its dependencies.

Run the container: Use the docker run command to start a new container from the image. You can use the -p option to map the container's ports to the host's ports and the -v option to mount a volume if needed.

Here is an example Dockerfile that you can use as a starting point:

FROM openjdk:8-jdk-alpine
COPY target/*.jar app.jar
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]


This Dockerfile uses the openjdk:8-jdk-alpine image as a base and copies the executable jar file into the container. It also sets some environment variables and configures the entrypoint to run the jar file.

Then, you can build the image by running the command:

docker build -t my-spring-app .
This will build the image and tag it as 'my-spring-app'.

Finally, you can run a container from the image:

docker run -p 8080:8080 my-spring-app

This will start a new container and map port 8080 of the container to port 8080 of the host, so you can access the application from your host's browser


Dockerizing a Node.js application

It involves packaging the application and its dependencies into a Docker image, so it can be easily deployed and run in a container. Here's a general approach for doing this:

Create a Dockerfile: This is a script that contains instructions for building the Docker image. It specifies the base image, the application code, and any dependencies that need to be installed. The Dockerfile should include a command to copy the application code into the container and a command to install any necessary dependencies.

Build the image: Use the docker build command to build the image from the Dockerfile. This will create a new image that contains your application and all its dependencies.

Run the container: Use the docker run command to start a new container from the image. You can use the -p option to map the container's ports to the host's ports and the -v option to mount a volume if needed.

Here is an example Dockerfile that you can use as a starting point:

FROM node:14
COPY . .
RUN npm install
EXPOSE 3000
CMD ["npm", "start"]

This Dockerfile uses the node:14 image as a base and copies the application code into the container. It then runs npm install to install any necessary dependencies. It also exposes port 3000 and configures the container to run the command npm start when it starts up.

Then, you can build the image by running the command:

docker build -t my-node-app .
This will build the image and tag it as 'my-node-app'.

Finally, you can run a container from the image:

docker run -p 3000:3000 my-node-app
This will start a new container and map port 3000 of the container to port 3000 of the host, so you can access the application from your host's browser.

Docker on Personal Laptop

You can use Docker directly for development on your personal laptop. Docker provides a way to run your application and its dependencies in an isolated environment, which can be very useful during development. This way, you can easily test your application on different environments and configurations without having to set up each one individually on your laptop.

Here are a few examples of how you can use Docker for development:

Developing with a specific runtime: If your application requires a specific version of Node.js, Ruby, or other runtime, you can use a Docker image that includes that version. This way, you can develop your application on your laptop without having to worry about installing the correct version of the runtime.

Developing with a specific database: If your application requires a specific version of MySQL, PostgreSQL, or other database, you can use a Docker image that includes that version. This way, you can develop your application on your laptop without having to worry about installing the correct version of the database.

Developing with a specific environment: If your application requires a specific environment, such as a specific version of Linux or a specific set of dependencies, you can use a Docker image that includes that environment. This way, you can develop your application on your laptop without having to worry about configuring the correct environment.

You can also use volumes to mount your local code into the container, so the container can access the changes in real-time. Also, you can use docker-compose to set up multiple containers with different services, like databases, web servers, etc. This can be very useful during development because you can test your application with different configurations and services.

It's worth noting that running your application inside a container can simulate production environment but it does not replace all the test and validation that you should do on the target environment


Post a Comment

0 Comments