Run bash command before running container - bash

I want to run a pre-existing Docker image like so:
docker run -d --name cdt-selenium selenium/standalone-firefox:3.4.0-chromium
So there is no Dockerfile that I control for this image. However, I would like to copy some files into this container.
If I did control the Dockerfile, I would like to run these commands:
RUN mkdir -p /root/cdt-tests/csv-data
COPY ./csv-data/* /root/cdt-tests/csv-data
Is there a way to run those commands in the same line as the Docker run command above?
I tried this:
docker run -d --name cdt-selenium selenium/standalone-firefox:3.4.0-chromium
docker exec cdt-selenium mkdir -p /root/cdt-tests/csv-data
docker cp cdt-selenium:/root/cdt-tests/csv-data ./csv-data
but I get a permissions error on the docker exec line

All images have a FROM line, and that can be any other image. So you can make a Dockerfile with:
FROM selenium/standalone-firefox:3.4.0-chromium
USER root
RUN mkdir -p /root/cdt-tests/csv-data
COPY ./csv-data/* /root/cdt-tests/csv-data
USER seluser
that will build your own image with your commands run.
You'd build it and create your own tag:
docker build -t alexander/selenium:3.4.0-chromium .
And then run it:
docker run -d --name cdt-selenium alexander/selenium:3.4.0-chromium
Edit: the exec command you ran failed because docker runs this container as a different user. You can see that in their Dockerfile. To solve that, run the exec with the root user option (-u root):
docker exec -u root cdt-selenium mkdir -p /root/cdt-tests/csv-data

Related

Jenkins console does not show the output of command runs on docker container

Running below command to execute my tests on docker container
sudo docker exec -i 6d49272f772c bash -c "mvn clean install test"
Above command running on Jenkins execute bash. But Jenkins console does not show the logs for test execution.
I had a similar problem with docker start (which is similar to docker exec). I used the -i option and it would work fine outside Jenkins, but the console in Jenkins didn't show any output from this command. I replaced -i with -a similar to the following:
sudo docker container create -it --name container-name some-docker-image some-command
sudo docker container start -a container-name
sudo docker container rm -f container-name
The docker exec method doesn't have a -a option so possibly removing the -i option would work too (since you are not interacting with the container in Jenkins), so if that doesn't work than you can convert to the following commands and achieve similar results with standard out being captured.

Docker run fail with ruby

I have a little problem, when i run my container this:
docker run -it emails_request cucumber -t #teste_inserindo_email
It's ok.
But, when i run this:
docker run it emails_request
Where my #teste_inserindo_emails, is on my dockerfile
WORKDIR /app
COPY Gemfile .
RUN bundle install && bundle clean
COPY . /app
EXPOSE 80
RUN cucumber -t #teste_inserindo_email
#CMD ["cucumber", "-t", " #teste_inserindo_email"]
Not found, return:
$ docker run -t emails_request
irb(main):001:0>
Or:
$ docker run emails_request
Switch to inspect mode.
what's your question exactly? you can just run it manually by firing up a container with an interactive terminal from your image, and then run the commands you want, or have a script in the image (or mounted as a volume) and then pass the script as the entry command instead.
docker run -it IMAGE_ID bash (for running manual commands)
if you want to use a script instead, put an ENTRYPOINT script in your Dockerfile instead

Execute script when docker container start

I want container with "centos:latest" image to be started and should execute my script. The scripts are copied with docker cp commands.
docker create --name centos1 centos:latest
docker cp . 5db38b908880:/opt ---> scripts are in current directory, hence .
docker commit centos1 new_centos1 --> now new_centos1 image has scripts
Now I want to start new container with the scripts to be executed: I tried below commands:
docker run -ti --rm --entrypoint "cd /opt && deploy_mediainfo_lambda.sh" new_centos1:latest
docker run -ti --rm new_centos1:latest "cd /opt && deploy_mediainfo_lambda.sh"
Both of above commands failed with:
docker: Error response from daemon: OCI runtime create failed: container_linux.go:296: starting container process caused "exec: \"cd /opt && deploy_mediainfo_lambda.sh\": stat cd /opt && deploy_mediainfo_lambda.sh: no such file or directory": unknown.
ERRO[0000] error waiting for container: context canceled
if used bash command while starting container, I can run my script using 'execuateble path'/'execuatble name' inside container, but I can not do this while starting container on commandline
docker run -ti --rm new_centos1:latest bash
[root#c34207f3f1c4 /]# ./opt/deploy_mediainfo_lambda.sh
If used below command, which calls executable directly, it gives path error.
docker run -ti --rm new_centos1:latest "deploy_mediainfo_lambda.sh"
docker: Error response from daemon: OCI runtime create failed: container_linux.go:296: starting container process caused "exec: \"deploy_mediainfo_lambda.sh\": executable file not found in $PATH": unknown.
ERRO[0000] error waiting for container: context canceled
Also not sure about setting $PATH from commandline while starting the container.
I know, using Dockerfile this is achievable, like:
can set path using ENV,
can copy executables with ADD or COPY
run executables using CMD or ENTRYPOINT
How to achieves it using docker commandline?
Thanks melpomene.
Here is my bash script to automate script execution inside container, after copying them, all using docker commands.
# Start docker container
docker create --name mediainfo_docker centos:latest
# copy script files
docker cp . mediainfo_docker:/opt
# save container with the new image, which contains all scripts.
docker commit mediainfo_docker mediainfo_docker_with_scripts
# Now run scripts inside docker container
docker run -ti --rm mediainfo_docker_with_scripts:latest /opt/deploy_mediainfo_lambda.sh
Since deploy_mediainfo_lambda.sh is a script, first line of it is:
#!/bin/bash

Docker container doesn't start due to CMD and ENTRYPOINT

Dockerfile
FROM ubuntu
.........................
.........................
.........................
# install magento script
ADD script.sh /
RUN chmod +x /script.sh
ENTRYPOINT ["bash", "-c"]
CMD ["/script.sh"]
Docker Build
docker build -t test/magento2:1.0.0 .
Docker Run
docker run -it -d --name docfiletest -h www.hostname.net -e BASE_URL=http://www.hostname.net/ -v /u01/docker/volumes/2.1.6:/var/www/html -p 7012:80 --net mynetwork --ip 172.18.0.14 --privileged test/magento2:1.0.0
Docker logs
[bu#bu docker]$ docker logs docfiletest
[SUCCESS]: Magento installation complete.
[SUCCESS]: Magento Admin URI: /admin
script.sh run successfully
Docker ps
[bu#bu docker]$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5e3e593960f5 test/magento2:1.0.0 "bash -c /script.sh" 26 minutes ago Exited (0) 7 minutes ago docfiletest
It never get started.
I doubt here command -> "bash -c /script.sh" . I thought I'm missing something in CMD and ENTRYPOINT.
can anyone help me on this?

Shell script: Remove hello world docker container without knowing ID

I'm running hello world docker container in a shell script:
#!/bin/bash
sudo docker run hello-world
I'm doing this to verify if the installation of docker was correct. After this I would like to remove this container again. But as I don't know the ID of this new container I can't remove it:
sudo docker rm hello-world
... is failing.
In the docs of docker: Explore the Application it's described how you can do this.
When you docker run hello-world, the container gets created with a random name that you can check with the following command:
docker container ls --all
Under the NAMES column of the output of the command you can check the generated name (you can see below on my example image. peaceful_morse in my case).
Then you can use this name as a parameter when you call the docker remove command:
docker rm peaceful_morse
Image with all the steps:
Give the container a name:
sudo docker run --name hello-world-container hello-world
Then you can remove it by name:
sudo docker rm hello-world-container

Categories

Resources