Basic Docker commands
Creating Docker image file:
1.Create a file called
Dockerfile
. A Dockerfile is a manifest that describes the base image to use for your Docker image and what you want installed and running on it.
touch Dockerfile
2.Edit the
Dockerfile
you just created and add the following content.FROM ubuntu:12.04
# Install dependencies
RUN apt-get update -y
RUN apt-get install -y apache2
# Install apache and write hello world message
RUN echo "Hello World!" > /var/www/index.html
# Configure apache
RUN a2enmod rewrite
RUN chown -R www-data:www-data /var/www
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2
EXPOSE 80
CMD ["/usr/sbin/apache2", "-D", "FOREGROUND"]
FROM ubuntu:15.04
COPY . /app
RUN make /app
CMD python /app/app.py
Each instruction creates one layer:
FROM
creates a layer from theubuntu:15.04
Docker image.COPY
adds files from your Docker client’s current directory.RUN
builds your application withmake
.CMD
specifies what command to run within the container.
3.Build the Docker image from your Dockerfile.
docker build -t hello-world .
4.Run docker images to verify that the image was created correctly.
docker images --filter reference=hello-world
5. Run the newly built image. The
-p 80:80
option maps the exposed port 80 on the container to port 80 on the host system. For more information about docker run, go to the Docker run reference.docker run -p 80:80 hello-world
Note
Output from the Apache web server is displayed in the terminal window. You can ignore the "
Could not reliably determine the server's fully qualified domain name
" message.
6.Open a browser and point to the server that is running Docker and hosting your container.
- If you are using an EC2 instance, this is the Public DNS value for the server, which is the same address you use to connect to the instance with SSH. Make sure that the security group for your instance allows inbound traffic on port 80.
- If you are running Docker locally, point your browser to http://localhost/.
- If you are using docker-machine on a Windows or Mac computer, find the IP address of the VirtualBox VM that is hosting Docker with the docker-machine ipcommand, substituting
machine-name
with the name of the docker machine you are using.docker-machine ip
machine-name
You should see a web page with your "Hello World!" statement.
7. Stop the Docker container by typing Ctrl + c.
==================================================================
- One liner to stop / remove all of Docker containers:
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
2. finding docker host
docker inspect --format="{{ .NetworkSettings.IPAddress }}" "Hostname"
3. Details of docker container
docker inspect <container_name>
4. How to connect to a stopped docker .
- First you need to connect to it
docker start <container_name>
- Then login to it via
docker attach <container_name>
P.S : docker run -i -t <image> would actually create a new container and connect you to it.
Have you configured the dns in this image ? I would like to use svn.wavecrest.gi on it to checkout the code.
- DNS name resolution doesn’t work under container use IP Address for svn.wavecrest.gi à eg: https://10.10.10.4/home/svnroot/<branch name>
5. How about mounting the drives
- If you are looking more space on container you need mount the <docker host directory> will mount in <Docker container>
#Mount a Host Directory as a Container Volume:ro and rw
Eg: -v=[ ]: Create a bind mount with: [host-dir]:[container-dir]:[rw|ro].
Syntax
# docker run -i -t -v /home/wcuser/data:/tmp:rw localhost:5000/git2.0-serv /bin/bash
More examples: https://docs.docker.com/reference/commandline/cli/
No comments:
Post a Comment