I want to make a script run a series of commands in a Docker container and then copy a file out. If I use docker run to do this, I don't get back the container ID, which I would need for the docker cp. (I could try and hack it out of docker ps, but that seems risky.)
It seems that I should be able to
Create the container with docker create (which returns the container ID).
Run the commands.
Copy the file out.
But I don't know how to get step 2. to work. docker exec only works on running containers...
If i understood your question correctly, all you need is docker "run exec & cp" -
For example -
Create container with a name --name with docker run -
$ docker run --name bang -dit alpine
Run few commands using exec -
$ docker exec -it bang sh -c "ls -l"
Copy a file using docker cp -
$ docker cp bang:/etc/hosts ./
Stop the container using docker stop -
$ docker stop bang
All you really need is Dockerfile and then build the image from it and run the container using the newly built image. For more information u can refer to
this
A "standard" content of a dockerfile might be something like below:
#Download base image ubuntu 16.04
FROM ubuntu:16.04
# Update Ubuntu Software repository
RUN apt-get update
# Install nginx, php-fpm and supervisord from ubuntu repository
RUN apt-get install -y nginx php7.0-fpm supervisor && \
rm -rf /var/lib/apt/lists/*
#Define the ENV variable
ENV nginx_vhost /etc/nginx/sites-available/default
ENV php_conf /etc/php/7.0/fpm/php.ini
ENV nginx_conf /etc/nginx/nginx.conf
ENV supervisor_conf /etc/supervisor/supervisord.conf
#Copy supervisor configuration
COPY supervisord.conf ${supervisor_conf}
# Configure Services and Port
COPY start.sh /start.sh
CMD ["./start.sh"]
EXPOSE 80 443
Related
I created a Docker like:
FROM rikorose/gcc-cmake
RUN git clone https://github.com/hect1995/UBIMET_Challenge.git
WORKDIR /UBIMET_Challenge
RUN mkdir build
WORKDIR build
#RUN apt-get update && apt-get -y install cmake=3.13.1-1ubuntu3 protobuf-compiler
RUN cmake ..
RUN make
Afterwards I do:
docker build --tag trial .
docker run -t -i trial /bin/bash
Then I run an executable that saves a .png file inside the container.
How can I visualize the image?
You can execute something inside the container.
To see all containers you can run docker ps --all.
To execute something inside container you can run docker exec <container id> command.
Otherwise you can copy files from container to host, with docker cp <container id>:/file-path ~/target/file-path
Please mount a localhost volume(directory) with container volume(directory) in where you are saving your images.
now all of your images saved in container directory will be available in host or localhost mount directory. From there you can visualize or download to another machine.
Please follow this
docker run --rm -d -v host_volume_or-directory:container_volume_direcotory trial
docker exec -it container_name /bin/bash
I'm new to docker.
I'm setting up nginx server to serve static files inside a docker container. I'd like to enable nginx to start automatically on every startup inside a docker container.
I've tried changing ENTRYPOINT, CMD and crontab when building DockerImage. But these settings to run nginx on every startup works only the first time I "run" a container. When I "stop" the container and "start" it again, nginx does not start automatically inside the container.
I'm looking for a way to start nginx on every startup of a container and my first question is "is it possible to do this?"
My second question is about a container cycle. Given that there are not many discussions on this subject (all discussions are about automatically running a script or sth else at the moment of "run"), I wonder if it is more efficient to "run" and "kill" a container each time than just "stopping" and "starting" a container.
Here are the lines of code I tried for DockerImage (with crontab), which was my first try.
RUN apt-get install -y cron
COPY run_server /etc/cron.d/run_server
RUN chmod 0644 /etc/cron.d/run_server
RUN crontab /etc/cron.d/run_server
RUN touch /var/log/cron.log
CMD cron && tail -f /var/log/cron.log
run_server is a simple crontab config file which includes:
#reboot service nginx start
Since this was not the solution I was looking for (it worked only when I "ran" a conainer, not "stopped" and "started" a container) I tried with supervisor, too.
RUN apt-get -y install supervisor && \
mkdir -p /var/log/supervisor && \
mkdir -p /etc/supervisor/conf.d
ADD supervisor.conf /etc/supervisor.conf
CMD ["supervisord", "-c", "/etc/supervisor.conf"]
supervisor.confg contains:
[supervisord]
nodaemon=true
[program:run_server]
command=/usr/bin/python3.6 /home/server.py
autostart=true
directory=/home
redirect_stderr=true
But neither of them worked the way I wanted ..
my Dockerfile , container ( CentOS 8 and nginx ) linux mint 19.3 Docker version 19.03.4
# howto: Dockerfile
# CentOS 8 and nginx
# docker build -t centose .
# docker run -it -p 80:80 centose
# curl localhost
FROM centos:latest
# MAINTAINER їван
RUN yum -y install nginx
EXPOSE 80
CMD ["/usr/sbin/nginx", "-g", "daemon off;"]
#WORKDIR /usr/sbin/
I would try to update my docker file and enable the nginx service so it will be started during the next reboot.
Here is a couple of different ways:
RUN systemctl enable nginx
RUN service nginx start
Another way would be to add a bootstrap script that starts the service:
#!/bin/bash
sudo service nginx start
tail -f /var/log/nginx/error.log
Make sure the bootstrap.sh is executable i.e sudo chmod +x bootstrap.sh.
Then update your docker file:
COPY boostrap.sh /bin/.
CMD ["bootstrap.sh"]
you can apply that using docker restart policy
you can simply set it when creating the container or updating the created ones
examples: docker run -t -d --restart unless-stopped nginx
to update the created ones: docker update --restart unless-stopped {container ID}
I am trying to modify the dockerfile of alpine:3.4 to include running git commands and automatically run nginx. Here are the changes I am appending to the default dockerfile.
RUN apk update
RUN apk add git
RUN mkdir mygit
RUN cd mygit
RUN git clone 'some url'
RUN apk add sudo
RUN sudo apk add docker
RUN sudo docker run --rm --name nginx nginx
The git command executes successfully and the RUN apk add docker also runs successfully. However, RUN sudo docker run --rm --name nginx nginx
fails.
Here is the log.
Step 28/31 : RUN sudo apk add docker
---> Using cache
---> 1cdf3005ea4b
Step 29/31 : RUN sudo docker run --rm --name nginx nginx
---> Running in 6c8c03b8a97d
docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?.
You are trying to run docker in docker which is "not possible" by default. Why don't you extend the nginx image instead and add git there?
Anyway, this feels like a fool's errand. Instead you should have a building environment in which you would copy application data into a nginx container for instance. Don't try to put everything in one container.
For instance look at my example Dockerfile which is serving Jekyll based static site:
FROM nginx:1.13-alpine
COPY site/ /usr/share/nginx/html
COPY default.conf /etc/nginx/conf.d/default.conf
It is better to use one container for one service.
Use Docker compose for your use case.
For sharing data between two containers, you can always use something like volumes(which is persistent, your host too can use that). This will solve your problem.
I am learning docker these days, and starts with building mysql image by myself.
Dockerfile:
FROM centos
MAINTAINER Michael
ENV REFRESHED_AT 2016-07-29
RUN yum install -y mysql mariadb-server
VOLUME /var/lib/mysql
ENTRYPOINT ["/usr/libexec/mysqld", "--user=root"]
EXPOSE 3306
docker run command
docker run -d --name mysql -v /root/docker/mysql/data:/var/lib/mysql -p 3306:3306 michael/mysql
This gave an error because I have to exec mysql_install_db first to init DB. But I could not add RUN mysql_install_db in the Dockerfile since I want to use Volume as external data store.
So how should I do this ?
I know there's an official image named mysql. I just want to do this as practice.
UPDATE:
Thanks to #Pieter . I finally did this by using his solution which is provide another entrypoint.sh combines init & start scripts then make it as ENTRYPOINT in Dockerfile :
FROM centos
MAINTAINER Michael
ENV REFRESHED_AT 2016-07-29
RUN yum install -y mysql mariadb-server
VOLUME /var/lib/mysql
COPY entrypoint.sh /usr/local/bin/
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3306
entrypoint.sh
#!/bin/bash
if [ ! -d "/var/lib/mysql/mysql" ]; then #check whether the DB is initialized.
echo 'Initializing database'
mysql_install_db
echo 'Database initialized'
fi
/usr/libexec/mysqld --user=root
docker run
docker run -d --name mysql -v /root/docker/mysql/data:/var/lib/mysql -p 3306:3306 michael/mysql
And this gives a generic solution for such scenario.
I'm no expert but for all I know there is no way to run some script before ENTRYPOINT as ENTRYPOINT is literally the first thing your container runs.
What you can do is add a custom script to your docker container that contains both the mysql_install_db and mysqld instructions and use that as the entrypoint.
So your dockerfile might look like this.
FROM centos
MAINTAINER Michael
ENV REFRESHED_AT 2016-07-29
RUN yum install -y mysql mariadb-server
VOLUME /var/lib/mysql
COPY entrypoint.sh /
ENTRYPOINT ["/entrypoint.sh"]
EXPOSE 3306
entrypoint.sh would simply look like this.
/usr/libexec/mysql_install_db --user=root
/usr/libexec/mysqld --user=root
Note that the same technique is also used in the official mariadb docker image. See https://github.com/docker-library/mariadb/tree/d969a465ee48fe10f4b532276f7337ddaaf3fc36/10.1
Note that the official image combines ENTRYPOINT with CMD. As you're learning docker you might want to take a look at https://www.ctl.io/developers/blog/post/dockerfile-entrypoint-vs-cmd/ for more information on how ENTRYPOINT and CMD can be used together.
UPDATE
Assuming that mysql_install_db simply creates some files under /var/lib/mysql you should be able to run mysql_install_db as part of your docker build. (This doesn't mean that you should though -> see official mariadb image).
The docker run command initializes a newly created volume with any data that exists at the specified location within the base image. See https://docs.docker.com/engine/reference/builder/#/volume
I am having a weird problem.
I am not able to ssh to docker container having ip address 172.17.0.61.
I am getting following error:
$ ssh 172.17.0.61
ssh: connect to host 172.17.0.61 port 22: Connection refused
My Dockerfile does contain openssh-server installation step:
RUN apt-get -y install curl runit openssh-server
And also step to start ssh:
RUN service ssh start
What could be the issue?
When I enter into container using nsenter and start ssh service then I am able to ssh. But while creating container ssh-server doesn't seems to start.
What should I do?
When building a Dockerfile you would create an image. But you can't create an image with an already running ssh daemon or any running service else. First if you create a running container out of the image you can start services inside. E.g. by appending the start instruction to the docker run command:
sudo docker run -d mysshserver service ssh start
You can define a default command for your docker image with CMD. Here is an example Dockerfile:
FROM ubuntu:14.04.1
MAINTAINER Thomas Steinbach
EXPOSE 22
RUN apt-get install -y openssh-server
CMD service ssh start && while true; do sleep 3000; done
You can build an run this image with the following two commands:
sudo docker build -t sshtest .
sudo docker run -d -P --name ssht sshtest
Now you can connect to this container via ssh. Note that in the example Dockerfile no user and no login was created. This image is just for example and you can start an ssh connection to it, but not login.
In my opinion there is a better approach:
Dockerfile
FROM ubuntu:14.04.1
EXPOSE 22
COPY docker-entrypoint.sh /docker-entrypoint.sh
RUN chmod +x /docker-entrypoint.sh
RUN apt-get install -y openssh-server
ENTRYPOINT ["sh", "/docker-entrypoint.sh"]
# THIS PART WILL BE REPLACED IF YOU PASS SOME OTHER COMMAND TO docker RUN
CMD while true; do echo "default arg" && sleep 1; done
docker-entrypoint.sh
#!/bin/bash
service ssh restart
exec "$#"
Build command
docker build -t sshtest .
The benefit of this approach is that your ssh daemon will always start when you use docker run, but you can also specify optional arguments e.g.:
docker run sshtest will print default arg every 1 second
whether docker run sshtest sh -c 'while true; do echo "passed arg" && sleep 3; done' will print passed arg every 3 seconds
I had the same problem.
Luckily I could solve it by checking kenorb answer and adapting it to my Dockerfile:
https://stackoverflow.com/a/61738823/4058295
It's worth a try :)