How to modify an existing configuration file such as .ssh/config inside the container on ddev - ddev

I need this to simplify my drush alias configuration, so I can establish servers, jumpboxes and usernames/keys in the config file and keep everything else simpler.

Update: From ddev 1.10 this operation is even simpler, as documented on https://ddev.readthedocs.io/en/stable/users/extend/in-container-configuration/
To change the /home/.ssh/config, you just need to add a .ssh/config file in the .ddev/homeaditions folder and make sure to include the following lines:
UserKnownHostsFile=/home/.ssh-agent/known_hosts
StrictHostKeyChecking=no
-- The following is only valid for ddev < 1.10 --
This is the solution I've implemented:
Because I didn't want to handle multiline additions, I decided to add a config file that would be appended to the /home/.ssh/config file.
I added a custom docker compose file in the .ddev folder: docker-compose.volumes.yml:
version: '3.6'
services:
web:
volumes:
- "./config:/etc/custom-config"
Then created the config folder inside the .ddev folder and added a configuration file with the content I wanted: extra-config.txt
Then, simply added a post-start hook in the config.yml file as #rfay suggested:
hooks:
post-start:
- exec: bash -c 'cat /etc/custom-config/extra-config.txt >> /home/.ssh/config'
Watch out because the commands need to be wrapped on bash -c, otherwise it would just output stdout and not modify the file.

Related

I have tried and really need some help in understanding why docker file wont run this script

I am trying, as part of an exercise, to create an image and run a simple bash script. This is my Dockerfile:
FROM ubuntu
RUN chmod 700 .
#Create container to store file in
RUN mkdir doc-conatiner
# source then the destination of container in docker if I have one
COPY . /functionfibonnaci/doc-conatiner
#when conatiner starts what is the executable
CMD ["bash", "functionfibonnaci.sh"]
when I run docker run:
bash: functionfibonnaci.sh: No such file or directory```
No such file or direcotry
I have been at this for two days and just cant get this to work- so answers will be appreiacted.
As #KapilKhandelwal indicates in their answer, you're having trouble because the bash functionfibonnaci.sh command is looking for the script in the current directory, but you've never changed directories, so you're in the container filesystem's root directory.
I'd suggest updating this in a couple of ways:
On your host system, outside of Docker, make sure that the script starts with a "shebang" line; the very first line, starting at the very first character, should be #!/bin/sh (or if you have bash-specific extensions and can't remove them, #!/bin/bash, but try to stick to POSIX shell syntax if you can).
On your host system, outside of Docker, make sure the script is executable; chmod +x functionfibonnaci.sh. With this and the previous step, you'll be able to just run ./functionfibonnaci.sh without explicitly mentioning the shell.
In the Dockerfile, change WORKDIR to some directory early. Often a short directory name like /app works well.
You don't need to RUN mkdir the WORKDIR directory or directories you COPY into; Docker creates them for you.
When you COPY content into the Dockerfile, the right-hand side can be a relative path like ., relative to the current WORKDIR, so you don't need to repeat the directory name.
In your CMD you can also specify the script location relative to the current directory.
These updates will get you:
FROM ubuntu
# do not need to mkdir this directory first
WORKDIR /app # or /functionfibonnaci/doc-conatiner if you prefer
# copy the entire build-context directory into the current workdir
COPY . .
# the command does not need to explicitly name the interpreter
# (assuming the script has a "shebang" line and is executable)
CMD ["./functionfibonnaci.sh"]
From the error message, it is clear that functionfibonnaci.sh is not found.
Update the CMD command in the Dockerfile to this:
CMD ["bash", "/functionfibonnaci/doc-conatiner/functionfibonnaci.sh"]
Note: This will work if the functionfibonnaci.sh file is in the same directory where the Dockerfile is present on the host machine. If it is present in a different directory, feel free to update the path of the file in the CMD accordingly.
TL;DR
Let's look closely what you are trying to do. The first two lines of the Dockerfile are self-explainatory.
In the third command, you are creating a directory with the intention to copy your script files. Sounds good so far!!!
The fourth line of the Dockerfile is what created a mess IMO. You are actually copying all the files from host to the directory /functionfibonnaci/doc-conatiner. But wait, you were supposed to copy those file inside the doc-conatiner directory that you created earlier.. right?
Now in the last line of the Dockerfile, you are trying to run the bash script functionfibonnaci.sh. But now, since the default WORKDIR is / by default, it will search for the functionfibonnaci.sh file inside the / directory. This file is actually present inside the /functionfibonnaci/doc-conatiner directory.
Hence, you are facing this issue.

Dockerfile - copying a bash file from host to Dockerfile

I'm trying to copy a bash file called setup_envs.sh which is in the same directory of my Dockerfile.
How can I run this bash file only once after Dockerfile is created?
My code is (in the end of the Dockerfile):
RUN mkdir -p /scripts
COPY setup_env.sh /scripts
WORKDIR /scripts
RUN chmod +x /scripts/setup_env.sh
CMD [./scripts/setup_env.sh]
Current error:
/bin/bash: [./scripts/setup_env.sh]: No such file or directory
I don't have a type in the file btw, I checked this.
Moreover, after I solve this and run the image to create a container - how can I make sure this bash script is only called once? Should I just write a command in the bash script that checks if some folder exists - and if it does - don't install it?
Based on the different comments including mine, this is what your Dockerfile extract should be replaced with:
COPY --chmod 755 setup_env.sh /scripts/
WORKDIR /scripts
CMD /scripts/setup_env.sh
Alternatively you can use the exec form for CMD but there is not much added value here since you're not passing any command line parameters.
CMD ["/scripts/setup_env.sh"]
At this point, I'm not really sure the WORKDIR instruction is useful (it depends on the rest of your Dockerfile and the content of your script).
Regarding your single bash script execution, I think you need to give a bit more background on the exact goal you are targeting. I have the feeling you could be in an X/Y Problem. And since this is a totally different issue, it should go inside a new question anyway with all required details.

sourcing a setup.bash in a Dockerfile

I am trying to build me a Dockerfile for my ROS project.
In ROS it is required that you source a setup bash in every terminal before starting to work.
(You can replace this by putting the source command in your bashrc file)
So, what I do is to source the file in the Dockerfle so that it gets run when the container is built. It works fine on that terminal
However when I open another terminal , predictably it seems that that file is not sourced and I have to do it manually.
Is there any way I can avoid this?
As I said in a non docker way, you put this into a file that gets called everytime a terminal is open but how do you do this with docker?
(in other words, how do you make sure a sh file is executed everytime I execute (or attach to) a docker container)
In your Dockerfile, copy your script to Docker WORKDIR:
COPY ./setup.bash .
Then set the entry point to run that script at container launch:
ENTRYPOINT ["/bin/bash", "-c", "./setup.bash"]
Note that with this approach, you won't be able to start your container in an interactive terminal with docker run -it. You'll need to do a few more things if that's what you want. Also, this will overwrite your original image's ENTRYPOINT (which you can find by docker image history), so make sure that is not essential. Otherwise, sourcing the script may be the better option for both cases:
RUN source ./setup.bash
Just add the script to startup configuration files in bash...
COPY ./setup.bash /etc/
RUN echo "source /etc/setup.bash" >> /etc/bash.bashrc
ENTRYPOINT /bin/bash
The file /etc/bash.bashrc might be named /etc/bashrc, or you might want to use /etc/profile.d directory, depending if you want the file to be sourced in interactive shells or not. Read the relevant documentation about startup files in bash.

difference in how mv command acts in dockerfile and on my local linux

I am looking at the following jboss/wildfly docker file which has the following command:
mv $HOME/wildfly-$WILDFLY_VERSION $JBOSS_HOME
where $WILDFLY_VERSION=17.0.1.Final and $JBOSS_HOME=/opt/jboss/wildfly.Thus the resulting command translates to:
mv $HOME/wildfly-17.0.1.Final /opt/jboss/wildfly
Later in the file we start the wildfly like this:
CMD ["/opt/jboss/wildfly/bin/standalone.sh", "-b", "0.0.0.0"]
So I assume that the mv command from the above took the content of $HOME/wildfly-17.0.1.Final directory and put it inside the /opt/jboss/wildfly directory.
However, if I try to recreate the steps from the dockerfile on my local machine (ubuntu 18.04) when I do mv $HOME/wildfly-17.0.1.Final /opt/jboss/wildfly, I end up with the following directory structure /opt/jboss/wildfly/wildfly-17.0.1.Final. That is, the wildfly-17.0.1.Final directory itself is copied into /opt/jboss/wildfly, rather than its content.
Can somebody please explain why I get this result locally?
$JBOSS_HOME path doesn't exist.
What the mv command does is actually replace the non-existent directory with the $HOME/wildfly-17.0.1.Final folder. That been said, the content of $HOME/wildfly-17.0.1.Final/ will be moved into $JBOSS_HOME replacing the name of the folder from "wildfly-17.0.1.Final" into "wildfly".
Simple Example would be as follow:
Create an empty dir and touch a file inside it, then try to do:
mv dir /var/lib/non_existing_folder
dir will be moved as is and replace "non_existing_folder".

Where is PHP.ini in Mac OS X Lion?

I wanted to run some PHP right on my Mac, uncommented httpd.conf, activated web sharing, installed MySQL etc.
I can't seem to find my PHP files, most importantly, PHP.ini.
On my old machine it was located in /usr/local/php5/lib
But php5 directory doesn't exist in /usr/local..
Do I need to get a package or am I looking in the incorrect place?
Running OS X 10.7.3, PHP 5.3.8, Apache/2.2.21
To locate the ini file on your machine, open Terminal.app and run the following command:
php --ini
If you need a template for Lion, try this.
You should find it in /private/etc if it exists, otherwise:
sudo cp /private/etc/php.ini.default /private/etc/php.ini
In terminal do php -i | grep php.ini.
Should give you some clues ;)
Answers from #Cronk and #Justin got me close on Mac OS X 10.9 Mavericks. In fact, on my system the /etc/php.ini file was missing completely, and it wasn't until I ran phpinfo() on the web server that I observed there was no configuration file. Copying the file from /etc/php.ini.default and adding the fully qualified path to the mcrypt.so library to the config file solved the problem:
cp /etc/php.ini.default /etc/php.ini
Then in /etc/php.ini add the following line (using the correct path to your mcrypt.so file):
extension="/usr/local/Cellar/php54-mcrypt/5.4.29/mcrypt.so"
Then restart Apache2:
apachectl restart
As pointed out, the php --ini command will tell you where PHP is expecting to find the php.ini file.
For a standard installation, it's likely to be looking for /etc/php.ini
If you've used macports then PHP may be looking for /opt/local/etc/php5/php.ini
Run phpinfo() from any file and it tells you where it is. Here is a screenshot.
I have more than once instance of PHP running so the other answers did not work for me. This did:
Create a PHP file and open its local url in your browser:
<?php phpinfo(); ?>
The PHP.INI path will be listed close to the top.
1- Find .ini location path
Open Terminal and run command
php --ini
2- copy this path /usr/local/etc/php/7.4/php.ini
and open it by command
nano /usr/local/etc/php/7.4/php.ini
3- Make changes and Quit with the keyboard combination Ctrl+X to exit nano
4- run apachectl restart after finish
This is rather old thread, but I would like to add a further clarification.
If you have a system that has multiple PHP installations (like mine did) the results you get from using the command line tools MAY BE different than what is actually used by the web server. If you are interested in what PHP is being used by the web server, only use the information gathered from a web page that uses the 'phpinfo()' function.
For example, on my system the versions reported by the two different methods were:
Command line: 5.3.26
Web: 5.3.13
For the php.ini file path things were different, too:
Command line: /private/etc/php.ini
Web: /long/path/to/the/file/php.ini
You can find all the possible php.ini files using this:
find / -name php.ini 2>&1 | grep -v "denied" | grep -v "directory"
(the '2>&1' and 'grep' elements just restrict the output of the command to useful information, i.e. get rid of error messages)
On my system I found 3 INI files. Because I was interested in what the web server was using I knew that I should use the path returned from the web-page results. I made my changes to the php.ini file that was listed there, restarted apache, and re-ran the web page; the new edits were shown there.
To restart apache:
sudo apachectl -k restart
-- J
On OSX/MacOS do the following in a Terminal window:
Run php --ini at the prompt by typing it and pressing enter
Reports something like:
Configuration File (php.ini) Path: /etc
Loaded Configuration File: (none)
Scan for additional .ini files in: /Library/Server/Web/Config/php
Additional .ini files parsed: (none)
...this is because in /etc there is a file called /etc/php.ini.default as an example and to show it is not in use. You need to copy that file to the name php expects so that you can edit it, like this:
Type:
$ sudo cp /etc/php.ini.default /etc/php.ini (and enter your password)
...then you will see if you run php --ini again that it now sees your new file:
Typing this: php --ini at the prompt should report this:
Configuration File (php.ini) Path: /etc
Loaded Configuration File: /etc/php.ini
Scan for additional .ini files in: /Library/Server/Web/Config/php
Additional .ini files parsed: (none)
...now edit /etc/php.ini - you want to make sure the following lines (NOT the same line starting with a semi-colon ';') are exactly as follows:
log_errors = On (this will turn the logging engine on)
Then, in this section:
; Log errors to specified file. PHP's default behavior is to leave this value
; empty.
; http://php.net/error-log
; Example:
;error_log = php_errors.log
; Log errors to syslog (Event Log on Windows).
;error_log = syslog
If you want to log to the syslog (or Windows Event Log on Windows) then ;error_log = syslog should become error_log = syslog
However, if as you say, you want to log to a file, you uncomment by removing the leading semi colon to make ;error_log = php_errors.log become error_log = php_errors.log or using a full path to place it where you want.
Good luck
I start with the 'Hello World!', once I get that displaying in my browser I throw a phpinfo(); in there and you can see all of the things. Tells you which configurations are loaded, everything.
<?php
echo 'Hello World!';
phpinfo();
You run php_info() and see line bellow :)
Loaded Configuration File /Applications/XAMPP/xamppfiles/etc/php.ini
Here is the complete method to modify correct php.ini in mac.
Find out the ini location via php --ini, this will give something like below:
Configuration File (php.ini) Path: /usr/local/etc/php/7.4
Loaded Configuration File: /usr/local/etc/php/7.4/php.ini
Scan for additional .ini files in: /usr/local/etc/php/7.4/conf.d
Additional .ini files parsed: /usr/local/etc/php/7.4/conf.d/ext-opcache.ini
Now open the php.ini via sudo vi /usr/local/etc/php/7.4/php.ini
Make your changes and exit it via :wq
Run final command sudo apachectl restart
You are changes should be now reflected, if done correctly :)

Resources